首页 > 解决方案 > 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()

标签: c#asp.net-mvcrepository-pattern

解决方案


如评论中所述,您的视图中似乎没有表格。您可以使用以下方法添加一个:

   @using (Html.BeginForm()) {
       <!-- form contentshere -->
   }

BeingForm 也有几个重载,您可以使用它们来更改它的去向、表单方法 (GET/PUT) 和设置 html 属性。


推荐阅读