我有一个下拉列表(可以选择多个选项)和另一个div中的两个输入框,其中包含id' showInvestmentForm'。选择下拉项目,我想生成这些文本关于所选下拉项数量的框。 我希望这些文本框具有不同的名称,但我无法找到一种方法来为它们提供不同的名称,使其输入值传递给控制器。
这是我的控制器的一部分: -
foreach(var data in propertyViewModel.PropertyInvestors)
{
FounderInvestment founderInvestment = new FounderInvestment
{
Id = propertyViewModel.FounderInvestmentViewModel.Id ?? 0,
InstallmentPeriod = propertyViewModel.FounderInvestmentViewModel.InstallmentPeriod,
InvestorId = Convert.ToInt32(data),
PropertyId = property.Id,
Investment = propertyViewModel.FounderInvestmentViewModel.Investment
};
_founderInvestmentQueryProcessor.Create(founderInvestment);
}
这是我的下拉菜单: -
<div class="form-group">
@Html.LabelFor(model => model.PropertyInvestors, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.PropertyInvestors, (IEnumerable<SelectListItem>)@ViewBag.Investors, "Select", htmlAttributes: new {@id="dropdown", @class = "form-control",@multiple="multiple" })
@Html.ValidationMessageFor(model => model.PropertyInvestors, "", new { @class = "text-danger" })
</div>
</div>
这些是文本框: -
<div id="showInvestmentForm" style="display:none">
<div class="form-group">
@Html.LabelFor(model => model.FounderInvestmentViewModel.Investment, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FounderInvestmentViewModel.Investment, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FounderInvestmentViewModel.Investment, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FounderInvestmentViewModel.InstallmentPeriod, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FounderInvestmentViewModel.InstallmentPeriod, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FounderInvestmentViewModel.Investment, "", new { @class = "text-danger" })
</div>
</div>
</div>
答案 0 :(得分:2)
如果我理解正确您的问题是输入文本没有发布到您的控制器。 如果你编写Controller的代码来检查完整的场景,那将会很有用。
无论如何,当你使用Mvc助手时,一个常见的错误就是你的情况,当你指向一个嵌套属性时,生成的输入名称不是最终的属性名,而是该属性的路径。
@Html.EditorFor(model => model.FounderInvestmentViewModel.InstallmentPeriod, ..)
在您的情况下,代码生成名为FounderInvestmentViewModel_InstallmentPeriod的输入,而不仅仅是InstallmentPeriod。
如果在您的控制器中,您需要一个名为&#34; InstallmentPeriod&#34;的参数。不会收到,因为该页面发布了一个名为&#34; FounderInvestmentViewModel_InstallmentPeriod&#34;
的参数