以下是控制器:
[HttpPost]
public ActionResult Save(HelloWorldMvcApp.ProductData model, string[] imgToDelete)
{
return Json(new { success = true });
}
以下是视图:
@model HelloWorldMvcApp.ProductData model
@{
ViewBag.Title = "Edit";
Layout = "~/Views/Shared/_SharedLayout.cshtml";
}
<h2>Edit</h2>
@using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "divProductData", OnSuccess = "hideModal", Url = Html.Action("Save").ToHtmlString() }))
{
<div id="divProductData">
<table>
<tr>
<td>
@Html.HiddenFor(a => a.id)
</td>
<td>
@Html.TextBoxFor(a => a.productDesc)
</td>
</tr>
<tr>
@foreach (string path in Model.picture.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries))
{
<td>
<input type="checkbox" name="imgToDelete" value="@path">
<br />
<img src='~/Images/Product/@path' />
</td>
}
</tr>
</table>
</div>
}
@section scripts{
<script>
function hideModal() {
$('.modal').modal('hide');
$('body').removeClass('modal-open');
$('.modal-backdrop').remove();
}
</script>
}
@using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "divProductData", OnSuccess = "hideModal", Url = Html.Action("Save").ToHtmlString() }))
引发了一个例外:
{&#34;一种公共行动方式&#39;保存&#39;在控制器上找不到 &#39; HelloWorldMvcApp.ProductDataController&#39;&#34;}
这里是代码的小提琴: https://dotnetfiddle.net/OLEq4o
如何解决此错误?
我尝试使用Ajax.BeginForm而不是Html.BeginForm,因为此页面显示在Bootstrap模式中,经过一些研究后,我无法调用&#34; hideModal&#34;函数,如果我使用Html.BeginForm
Close modal window containing ASP MVC Ajax form
如果我错了,请纠正我
答案 0 :(得分:0)
哦......我想通了,首先,我将动作名称移到了AjaxOptions之外,然后我将[HttpPost]
属性更改为控制器中的[AcceptVerbs(HttpVerbs.Get)]
,并添加了JsonRequestBehavior.AllowGet
in JSON结果,结果如下:
答案 1 :(得分:0)
尝试指定控制器名称。
@using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "divProductData", OnSuccess = "hideModal", Url = Html.Action("Save","controllerName").ToHtmlString() }))