只有部分视图表单的值没有传递给控制器。:/ FounderInvestmentVM是我创建的部分视图,这个VM在PropertyVM内部。其他值传递给控制器但不是部分视图。当我放入调试器并看到它时,它总是给FounderInvestments Count=0
:/
这是我的包含FounderInvestorVM的PROPERTY VM: -
namespace propertyMgmt.ViewModel.PropertyViewModel
{
public class PropertyViewModel
{
public int? Id { get; set; }
[Required]
[DisplayName("Property Title")]
public string PropertyTitle { get; set; }
......
public List<FounderInvestmentViewModel> FounderInvestments { get; set; }=new List<>(FounderInvestmentViewModel);
}
}
这是FounderInvestorVM: -
public class FounderInvestmentViewModel
{
public int? Id { get; set; }
public int PropertyId { get; set; }
public int InvestorId { get; set; }
public double Investment { get; set; }
public int InstallmentPeriod { get; set; }
public IEnumerable<SelectListItem> FounderInvestorList { get; set; }
}
这是我的编辑: -
public ActionResult Create(PropertyViewModel _propertyViewModel)
{
if (ModelState.IsValid)
{
Property property = new Property();
property.Id = _propertyViewModel.Id ?? 0;
property.PropertyTitle = _propertyViewModel.PropertyTitle;
........other properties......
}
_propertyQueryProcessor.Create(property);
foreach(var investment in _propertyViewModel.FounderInvestments)
{
FounderInvestment _founderInvestment = new FounderInvestment
{
Id = investment.Id??0,
InstallmentPeriod = investment.InstallmentPeriod,
InvestorId = investment.InvestorId,
PropertyId = investment.PropertyId,
Investment = investment.Investment
};
_founderInvestmentQueryProcessor.Create(_founderInvestment);
}
这是部分视图: -
@model propertyMgmt.ViewModel.FounderInvestmentViewModel
@{
ViewData.TemplateInfo.HtmlFieldPrefix = "PropertyViewModel"; //bind to main model
}
<div class="founderInvestmentDetails">
@using (Html.BeginCollectionItem("founderInvestmentDetails"))
{
@Html.HiddenFor(m => m.Id, new { @class = "id" })
@Html.HiddenFor(m=>m.PropertyId, new { @name = "PropertyId" })
<div class="form-group">
@Html.LabelFor(m => m.FounderInvestorList, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(m=>m.FounderInvestorList,Model.FounderInvestorList , "Select Investor", htmlAttributes: new {@class = "form-control"})
@Html.ValidationMessageFor(m => m.FounderInvestorList, "", new { @class = "text-danger" })
@Html.HiddenFor(m => m.InvestorId, new { @name = "InvestorId" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Investment, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(m=>m.Investment, new { htmlAttributes = new { @class = "form-control",@type="number" } })
@Html.ValidationMessageFor(m => m.Investment, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.InstallmentPeriod, htmlAttributes: new { @class = "control-label col-md-2", @type = "number" })
<div class="col-md-10">
@Html.EditorFor(m => m.InstallmentPeriod, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(m => m.InstallmentPeriod, "", new { @class = "text-danger" })
</div>
</div>
}
</div>
最后这是主要的视图: -
@model propertyMgmt.ViewModel.PropertyViewModel.PropertyViewModel
@using (Html.BeginForm("Create", "Property", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="form-group">
@Html.LabelFor(model => model.PropertyTitle, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PropertyTitle, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PropertyTitle, "", new { @class = "text-danger" })
</div>
</div>
.....(other form groups)
<div id="founderInvestmentDetails" class="form-group">
@foreach(var founderInvestmentDetails in Model.FounderInvestments)
{
@Html.Partial("_FounderInvestmentDetails", founderInvestmentDetails)
}
</div>
答案 0 :(得分:1)
抱歉,这里有一个愚蠢的错误。正如@Stephen Muecke指出的那样
@using (Html.BeginCollectionItem("founderInvestmentDetails"))
应该是
@using (Html.BeginCollectionItem("FounderInvestments"))
因为BeginCollectionItem使用它正在处理的Collection
的名称。