为什么我的按钮没有调用控制器中的操作?

Why my button is not calling action in the controller?

我有 EDITSTORIES 部分视图,它必须 post 数据到 Stories 控制器中的 UpdateStories 操作,但它没有。它甚至没有达到断点。

@using (Html.BeginForm("UpdateStories", "Stories", FormMethod.Post, new{enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Stories</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.ID)

        <div class="form-group">
            @Html.LabelFor(model => model.Image, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Image, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Image, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Story, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Story, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Story, "", new { @class = "text-danger" })
            </div>
        </div>


        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Update" class="btn btn-default" />
            </div>
        </div>
    </div>
}

操作:

   [HttpPost]
        public ActionResult UpdateStories(Stories st)
        {    
            ViewBag.Grid= bo.GetAllImages();

            if (bo.UpdateImages(st))
            {
                ViewBag.Data = "Updated successfully";
            }
            else 
            {
                ViewBag.Data = "Update failed";
            }

            ViewBag.Style = "display:none";    
            return View("GetStories", st);   
        }
    }

它位于主视图 GetStories 中。这是漫长的一天,但还没有完成。请帮助我。

更新:

路线:

  routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Stories", action = "AddStories", id = UrlParameter.Optional }
            );
            routes.MapRoute(
                name: "ShowStories",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Stories", action = "ShowStories", id = UrlParameter.Optional }
            );

更新:

查看:GetStories

@model HimHer.Models.Stories

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@using (@Html.BeginForm("GetStories", "Stories", FormMethod.Get))
{
        @Html.AntiForgeryToken()

    <div style="@ViewBag.Style">
        @{
            Html.RenderPartial("EditStories", Model);
        }


    </div>

    <hr />
    var listData = (List<HimHer.Models.Stories>)ViewBag.Grid;

    WebGrid wgImages = new WebGrid(listData, rowsPerPage: 20);
    @wgImages.GetHtml(tableStyle: "table table-condensed table-bordered table-striped table-responsive",
columns: wgImages.Columns(
                      wgImages.Column
                      (columnName: "Image", header: "Image"),
                      wgImages.Column
                      (columnName: "Story", header: "Story"),
                      wgImages.Column
                      (columnName: "Image", header: "Download", format: (testItem) => Html.ActionLink("Download", "DownloadStories", new { filename = testItem.Image })),
                      wgImages.Column
                                         (header: "Edit", format: (testitem) => Html.ActionLink("Edit", "EditStories", new { ID = testitem.ID, Story = testitem.Story, Image = testitem.Image, HiddenID = 1 }))
                     )
);



}

<h2>Index</h2>

您的代码将生成 2 个表单,它们是嵌套的!。

<form action="/Stories/GetStories">
   <form action="/Stories/UpdateStories">
      <input type="submit" />
   </form>
</form>

嵌套表单无效!这就是为什么当你从局部视图点击内部表单的提交按钮时,它提交给为外部定义的动作方法表格.

你不应该嵌套表格。因此,将对 RenderPartial 的调用移到 BeginForm 调用之外。

查看您共享的代码,主视图中不需要表单标记,因为您没有必须提交的任何表单数据。所以只需删除它。

如果您绝对想在主视图中使用另一个窗体,请确保它不会创建嵌套窗体情况。您可以在同一视图中同时显示 2 个表单

@using (@Html.BeginForm("GetStories", "Stories", FormMethod.Get))
{
   <!-- Some form elements needed for this form -->     

}

@{ Html.RenderPartial("EditStories", Model); }