下拉列表在部分视图中返回null viewdata

时间:2015-03-02 21:15:09

标签: asp.net asp.net-mvc-4 html.dropdownlistfor asp.net-mvc-partialview ajax.beginform

我正在开发一个多步骤表单,它必须在其他步骤加起来之前提交数据库的第一步,最后在最后一步之后提交。看看选项我决定使用会话(但是我会很乐意使用更好的替代方案)。我设法让这个工作,后来决定使用ajax表单提交因此需要partialviews。问题是下拉列表返回null viewdata值 - 即它们没有绑定。任何人都可以建议在控制器中进行组合的最佳方法。如果我恢复返回视图而不是返回部分视图,它可以正常工作。

控制器

[AllowAnonymous] 
public ActionResult ContactViewDetails()
         {
ViewBag.CountryList = new SelectList(db.Countries, "CountryId", "CountryName");
             return PartialView("ContactViewDetails");
}

模型

 public int CountryId{get;set;}
  public virtual Country Country { get; set; }
  .......and others

默认页面视图

<script src="~/Scripts/jquery-2.1.3.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>

<div id="divContainer"> 
     @Html.Partial("RegViewDetails")
 </div>

PartialView:ContactViewDetails.cs

@{
AjaxOptions options = new AjaxOptions();
options.HttpMethod = "POST";
options.InsertionMode = InsertionMode.Replace;
options.UpdateTargetId = "divContainer";
}
  @using (Ajax.BeginForm("ContactViewDetails", "OnlineApplication", options, new { @class = "form-horizontal" }))
                {
    @Html.DropDownListFor(x => x.CountryId, (IEnumerable<SelectListItem>)ViewBag.CountryList, new { @class = "chooseOption" })
 @Html.......others
 }

ContactViewDetails页面是表单连续的第二步第一步是RegViewDetails页面,您可以在Default页面视图中看到。验证后RegViewDetails返回ContactViewDetails Partial

......
return PartialView("ContactViewDetails");

0 个答案:

没有答案