我知道名称在 HtmlHelper 中的方式是破坏模型绑定的原因。但我还没有想出如何解决它。或者,即使有修复。
一点解释。我使用一种模型来填充:文本、控件、默认值(选择列表)和保存的值。我使用另一个 ViewModel 只返回:值、dataid 和 ctrltypeid。被注释掉的是导致回发 ViewModel 未被填充的代码(例如:Html.TextBoxFor)。工作代码(例如:Html.TextBox)按预期工作。所以我想知道我必须对注释代码做什么才能使其像未注释代码一样工作。真的有什么我可以做的吗?
@model InspectionWebFormsMVC.ViewModels.FormRowModel
@using (Html.BeginForm("Index", "Section", FormMethod.Post))
{
<div style="clear:both; padding:1%;">
<div class="section">
@Model.Section
</div>
<div class="number">
@Model.SectionNumber
</div>
<div class="desc">
@Model.Description
</div>
<div class="ctrl">
@{
int i = 0;
//for (int i = 0; i < formRow.RowInput.Count; ++i)
foreach (var RowInput in Model.RowInput)
{
var ddv = new SelectList(RowInput.RowCtrl.DefaultValues, "Value", "Label");
switch (RowInput.RowCtrl.Type)
{
case "dropdown":
//@Html.DropDownListFor(blah => RowInput.InputtedData, ddv)
//@Html.HiddenFor(blah => RowInput.InputtedDataID)
//@Html.HiddenFor(blah => RowInput.RowCtrl.CtrlTypeID)
@Html.DropDownList("InputtedData", ddv)
@Html.Hidden("InputtedDataID", RowInput.InputtedDataID)
@Html.Hidden("CtrlTypeID", RowInput.RowCtrl.CtrlTypeID)
<br />
break;
case "text":
//@Html.TextBoxFor(blah => RowInput.InputtedData)
//@Html.HiddenFor(blah => RowInput.InputtedDataID)
//@Html.HiddenFor(blah => RowInput.RowCtrl.CtrlTypeID)
@Html.TextBox("InputtedData", RowInput.InputtedData)
@Html.Hidden("InputtedDataID", RowInput.InputtedDataID)
@Html.Hidden("CtrlTypeID", RowInput.RowCtrl.CtrlTypeID)
<br />
break;
}
}
++i;
}
显然 Viewmodel 用于 Postback。
namespace InspectionWebFormsMVC.ViewModels
{
public class SaveKeyValueInput
{
public long InputtedDataID { get; set; }
public long CtrlTypeID { get; set; }
public string InputtedData { get; set; }
}
}
从控制器。用于测试。
[HttpPost]
public ActionResult Index(SaveKeyValueInput placeholder)
{
var ha = placeholder;
var la = "Hello";
FormRowProcessing placeholder2 = new FormRowProcessing();
var stub = placeholder2.stub()[2];
return View(stub);
}