c# - c# 编辑按钮的MVC代码,但是点击编辑按钮不贴
问题描述
我将以下代码用于编辑按钮,但单击编辑按钮不会发布它
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "PageID,GroupID,Title,ShortDescription,Text,Autor,Tags,Visit,ImageName,ShowInSlider,CreatDateTime")] Page page,HttpPostedFileBase imgUp)
{
if (ModelState.IsValid)
{
if (imgUp != null)
{
if (page.ImageName != null)
{
System.IO.File.Delete(Server.MapPath("/PageImages/" + page.ImageName));
}
page.ImageName = Guid.NewGuid() + Path.GetExtension(imgUp.FileName);
imgUp.SaveAs(Server.MapPath("/PageImages/" + page.ImageName));
}
pageRepository.UpdatePage(page);
pageRepository.save();
return RedirectToAction("Index");
}
……
我有单独的数据层和存储库,我将以下代码用于编辑页面控制器,但单击编辑按钮不会发布表单。虽然它适用于创建和删除 btn。我的视图代码是:
<div class="form-horizontal">
<h4>Page</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.PageID)
@Html.HiddenFor(model => model.Visit)
@Html.HiddenFor(model => model.CreatDateTime)
@Html.HiddenFor(model => model.ImageName)
<div class="form-group">
@Html.LabelFor(model => model.GroupID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("GroupID", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.GroupID, "", new { @class = "text-danger" })
</div>
</div>
....
<div class="form-group">
@Html.LabelFor(model => model.ImageName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="file" name="imgUp" id="imgUp"/>
@Html.ValidationMessageFor(model => model.ImageName, "", new { @class = "text-danger" })
@if (Model.ImageName != null)
{
<img src="/PageImages/@Model.ImageName" class="thumbnail" style="max-width:150px" />
}
.....
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
我跟踪我的代码并发现这个错误:
Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.Request","time":"2018-04-25T08:10:44.3663705Z","tags":{"ai.internal.sdkVersion":"web: 2.0.0.25000","ai.device.roleInstance":"Laptop-erfan","ai.operation.name":"GET PageGroup
上面的水平表单标签是这个代码:
@model DataLayer.Page
@{
ViewBag.Title = "Edit";}<h2>Edit</h2>@using (Html.BeginForm("Edit", "Pages", FormMethod.Post, new { enctype = "multipart/form-data" })){ @Html.AntiForgeryToken()
解决方案
如评论中所述,您的视图中似乎没有表格。您可以使用以下方法添加一个:
@using (Html.BeginForm()) {
<!-- form contentshere -->
}
BeingForm 也有几个重载,您可以使用它们来更改它的去向、表单方法 (GET/PUT) 和设置 html 属性。
推荐阅读
- vue.js - 如何在 vue good table 远程模式下以编程方式选择所有(不仅从第一页)行?
- c# - 将通用列表转换为通用 IEnumerable
- wordpress - 使用 ansible 在 Centos7 上设置 Wordpress
- laravel - Laravel 单元测试
- django - 为什么我的回复显示为普通评论,而不是对特定评论的回复?
- testing - 网站维护测试
- javascript - 如何从 package.json 中删除冗余/未使用的依赖项?
- spring - Spring Boot 获取身份验证主体
- python - 稀疏矩阵长度不明确
- git - 如何覆盖 git log --format=oneline 实际格式?