2012-07-05 117 views
0

我知道名称在HtmlHelper中的方式是打破模型绑定。但我还没有想出如何解决它。或者如果甚至有修复。这个模型绑定为什么不起作用?

有点解释。我使用一个模型填充:文本,控件,默认值(选择列表)和保存的值。我使用另一个ViewModel只返回:value,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; 
        } 

明显的Postback的Viewmodel。

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); 
    } 
+0

无关的忠告的话......我会推荐下面的MSDN套接字约定,w其中包括变量名称应该是骆驼式的,例如'var rowInput'而不是'var RowInput' – 2012-07-05 17:52:19

回答

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) 
    ... 
} 

或一个更好的办法是当然完全摆脱这个丑陋环路(用于或的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) 
{ 
    ... 
} 
+0

即使只有3个输入字段?其他一切都将是文本,并将返回空,0或空。 – dotnetN00b 2012-07-05 18:13:19

+0

可能你没有遵守正确的命名约定。 – 2012-07-05 19:04:36

+0

我遇到了一个问题。我想我会再写一篇关于它的文章。但是它的长短不一。我做了你所说的,现在我看到的只有一行:“88888”,行应该是这样。 – dotnetN00b 2012-07-06 22:37:09