0

我知道名称在 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);
    }
4

1 回答 1

3

以下是使注释代码工作的方法:

for (int i = 0; i < Model.RowInput.Count; i++)
{
    @Html.DropDownListFor(blah => blah.RowInputpi[i].InputtedData, ddv)
    @Html.HiddenFor(blah => blah.RowInput[i].InputtedDataID)
    @Html.HiddenFor(blah => blah.RowInput[i].RowCtrl.CtrlTypeID)
    ...
}

或者更好的方法当然是完全摆脱这个丑陋的循环(for 或 foreach)并用一行代码替换它:

@Html.EditorFor(x => x.RowInput)

现在剩下的就是定义相应的编辑器模板(框架将自动为 RowInput 集合的每个元素呈现,这样您就不必编写任何循环)。编辑器模板按惯例工作。因此,如果您的RowInput属性定义如下:

public IEnumerable<FooBar> RowInput { get; set; }

你定义模板~/Views/Shared/EditorTemplates/FooBar.cshtml

@model FooBar

... put your switches and cases and stuff, but I remove this noise to 
... make the answer more readable

@Html.DropDownListFor(blah => blah.InputtedData, ddv)
@Html.HiddenFor(blah => blah.InputtedDataID)
@Html.HiddenFor(blah => blah.RowCtrl.CtrlTypeID)

但是由于您的表单包含所有记录,因此您发布到的相应控制器操作必须与顶级视图模型一起使用,因为您正在发送所有内容:

[HttpPost]
public ActionResult Index(FormRowModel model)
{
    ...
}
于 2012-07-05T17:53:10.647 回答