我正在尝试提交由两个强类型视图组成的表单。这个问题很相似,但没有答案:
MVC 3 Razor Form Post w/ Multiple Strongly Typed Partial Views Not Binding
当我提交表单时,提交到控制器的模型始终为null。我花了几个小时来尝试使它工作。这似乎应该很简单。我错过了什么吗?我不需要做ajax,只需要能够发布到控制器并呈现一个新页面。
谢谢
这是我的视图代码:
<div>
@using (Html.BeginForm("TransactionReport", "Reports", FormMethod.Post, new {id="report_request"}))
{
ViewContext.FormContext.ValidationSummaryId = "valSumId";
@Html.ValidationSummary(false, "Please fix these error(s) and try again.", new Dictionary<string, object> { { "id", "valSumId" } });
@Html.Partial("_ReportOptions", Model.ReportOptions);
@Html.Partial("_TransactionSearchFields", new ViewDataDictionary(viewData) { Model = Model.SearchCriteria });
}
这是控制器中的代码:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult TransactionReport(TransactionReportRequest reportRequest)
{
var reportInfo = new List<TransactionReportItem>();
if (ModelState.IsValid)
{
var reportData = _reportDataService.GetReportData(Search.MapToDomainSearchCriteria(reportRequest.SearchCriteria));
if (reportData!=null)
{
reportInfo = reportData.ToList();
}
return View(reportInfo);
}
return View(reportInfo);
}
这些局部视图本身并不重要,因为它们所做的只是绑定和显示它们的模型。