【发布时间】:2016-11-23 18:57:12
【问题描述】:
您好,我有以下 Razor 视图:
@model AgonConFF.ViewModels.ClaimModel
@using (Ajax.BeginForm("DataCaptureNew", "Home", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "result", OnBegin = "onBegin()", OnComplete = "onComplate()" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.Origin.SourceName, new { htmlAttributes = new { @class = "form-control CapInput", placeholder = "Source Name" } })
@Html.ValidationMessageFor(model => model.Origin.SourceName, "", new { @class = "text-danger" })
@*Source Name*@
@Html.EditorFor(model => model.Origin.MailFax, new { htmlAttributes = new { @class = "form-control CapInput", placeholder = "Mail Address / Fax Nr" } })
@Html.ValidationMessageFor(model => model.Origin.MailFax, "", new { @class = "text-danger" })
@*Mail Address / Fax Nr*@
<input type="submit" value="Capture" class="btn btn-default" />
}
我的 JS 看起来像这样:
function onBegin() {
$('#loading').show();
}
function onComplate() {
$('#loading').hide();
}
我的控制器看起来像这样:
//Action method that handles the testCreate form submission
[HttpPost]
[ValidateAntiForgeryToken]
public PartialViewResult DataCaptureNew(ClaimModel origin)
{
if (ModelState.IsValid)
{
db.Origins.Add(origin.Origin);
origin.Origin.OriginID = 1;
db.SaveChanges();
return PartialView("testPartial", origin);
}
return PartialView("testPartial", origin);
}
当我提交表单并使用断点检查控制器内部时,我看到ClaimModel origin 的相关对象为空并给我一个错误。当我只使用Html.BeginForm 时,这篇文章非常有效。在这种情况下,Origin 对象可以很好地通过。如何使用Ajax.BeginForm 传递此对象?
【问题讨论】:
标签: ajaxform asp.net-mvc-5 ajax.beginform