部分视图计数 = 0,值未发送到控制器
Partial View Count=0, value not being sent to the controller
只有部分视图表单的值没有被传递给控制器。:/ FounderInvestmentVM 是我创建的部分视图的那个,这个 VM 在 PropertyVM.The 内,其他值被传递给控制器,但不是部分视图的控制器。当我放置调试器并看到它时,它总是给出 FounderInvestments Count=0
:/
这是我的 属性 包含 FounderInvestorVM 的 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>
抱歉,有一个愚蠢的错误here.As @Stephen Muecke 指出
@using (Html.BeginCollectionItem("founderInvestmentDetails"))
应该是
@using (Html.BeginCollectionItem("FounderInvestments"))
因为 BeginCollectionItem 使用它正在处理的 Collection
的名称。
只有部分视图表单的值没有被传递给控制器。:/ FounderInvestmentVM 是我创建的部分视图的那个,这个 VM 在 PropertyVM.The 内,其他值被传递给控制器,但不是部分视图的控制器。当我放置调试器并看到它时,它总是给出 FounderInvestments Count=0
:/
这是我的 属性 包含 FounderInvestorVM 的 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>
抱歉,有一个愚蠢的错误here.As @Stephen Muecke 指出
@using (Html.BeginCollectionItem("founderInvestmentDetails"))
应该是
@using (Html.BeginCollectionItem("FounderInvestments"))
因为 BeginCollectionItem 使用它正在处理的 Collection
的名称。