我有一個窗體(使用主視圖模型),然後是部分視圖來實現窗體的地址部分(輔助視圖模型)。當我提交表單時,我沒有回到表單的內部部分視圖部分的值。沒有得到任何錯誤,只是失去了部分的價值。代碼如下:失去部分視圖模型表格值
形式的「主」 部分:
@model REMS.ViewModels.AddComplexViewModel
@{
ViewBag.Title = "Add Complex";
}
<h2>@ViewBag.Title</h2>
@{ Html.Partial("~/Views/Shared/Partials/ActionStatusPartial.cshtml", Model.ActionStatusMessageViewModel); }
@using (Html.BeginForm("AddComplex", "Admin", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>Add a New Complex</h4>
<hr />
<div class="form-group">
@Html.LabelFor(m => m.Name, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
</div>
</div>
@Html.Partial("~/Views/Shared/Partials/AddressFormPartial.cshtml", Model.AddressViewModel)
<div class="form-group">
@Html.Label("Owner", new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.DropDownList("SelectedOwner", Model.Owners, "Select an Owner...")
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Add" />
</div>
</div>
}
形式的局部視圖:
@model REMS.ViewModels.AddressViewModel
<div class="form-group">
@Html.LabelFor(m => m.Address1, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.Address1, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Address2, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.Address2, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.City, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.City, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.State, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.State, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Zip, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.Zip, new { @class = "form-control" })
</div>
</div>
視圖模型:
public class AddComplexViewModel
{
[Display(Name = "Name")]
public string Name { get; set; }
[Display(Name = "AddressViewModel")]
public AddressViewModel AddressViewModel { get; set; }
[Display(Name = "SelectedOwner")]
public Guid SelectedOwner { get; set; }
[Display(Name = "Owners")]
public SelectList Owners { get; set; }
[Display(Name = "ActionStatusMessageViewModel")]
public ActionStatusViewModel ActionStatusMessageViewModel { get; set; }
public AddComplexViewModel()
{
ActionStatusMessageViewModel = new ActionStatusViewModel();
AddressViewModel = new AddressViewModel();
}
}
public class AddressViewModel
{
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}
當我提交的形式地址值全爲空。我錯過了什麼嗎?
局部視圖仍技術上來看,你不能夠從部分獲取值只是因爲它是主視圖的形式中。你可以使用像這裏概述的編輯器模板 - > https://stackoverflow.com/questions/5197038/complex-models-and-partial-views-model-binding-issue-in-asp-net-mvc-3 – ragerory
該解決方案工作。現在有其他問題,但與此無關。你可以添加這個答案,以便我可以將其標記爲已接受?謝謝! –