2015-02-24 108 views
1

我有以下CSHTML形式:Html.BeginForm()没有通过模型

model Scraper.Facade.PlayerRow 

@using (Html.BeginForm("Calculate", "Home", FormMethod.Post)) 
{ 
<table class="table table-striped table-bordered table-condensed table-responsive table-hover"> 
    @foreach (var player in Model.AttribsPlayerLine) 
    { 
     <thead> 
      <tr class="success"> 
       @foreach (var attrib in player.AttribsPlayerList) 
       { 
        //@Html.DisplayNameFor(x => Model.tytul) 
        <th data-field="@attrib.attribName">@Html.DisplayFor(x => attrib.attribName) </th> 
       } 
      </tr> 
      <tr class="active"> 
       @foreach (var attrib in player.AttribsPlayerList) 
       { 
        <td data-field="@attrib.attribValue">@Html.TextBoxFor(x => attrib.attribValue)</td> 
       } 
      </tr> 
     </thead> 

    } 
</table> 

<input class="btn-danger" type="submit" name="Next >>" value="Next >>" /> 
} 

这是正确显示,然后我试图让该模型在以下控制器的ActionResult

[HttpPost] 
public ActionResult Calculate(PlayerRow model) 
{ 
    GenericHelper _genericHelper = new GenericHelper(); 
    return View(); 
} 

然而,PlayerRow模型始终为空。

我在做什么错?

这是我的模型定义

public PlayerRow LoadHtmlDoc(string fileLocation) 
{ 
     List<Attrib> attribsHeaderList = new List<Attrib>(); 
     var playerRow = new PlayerRow(); 
     playerRow.AttribsPlayerLine = new List<AttribLine>(); 

     var htmlDoc = new HtmlDocument { OptionFixNestedTags = true }; 

     // There are various options, set as needed 

     // filePath is a path to a file containing the html 
     htmlDoc.Load(fileLocation); 
} 

public class PlayerRow 
{ 
    public List<AttribLine> AttribsPlayerLine; 

} 

UPDATE

所有嗨,改了一下我的应用程序的逻辑,基本上具有2所列出已页眉属性和属性对于所有的球员,所以现在只有2个类,我改变了这样的cshtml,现在正在工作: -

@using (Html.BeginForm("Calculate", "Home", FormMethod.Post, new { enctype =  "multipart/form-data" })) 
{ 
<table class="table table-striped table-bordered table-condensed table-responsive table-hover"> 
    <tr> 
     @for (int k = 0; k < Model.HeaderAttributes.Count; k++) 
     { 
      <td> 
       @Html.DisplayFor(x => x.HeaderAttributes[k].AttributeName) 
       @Html.HiddenFor(x => x.HeaderAttributes[k].AttributeName) 
      </td> 
     } 
    </tr> 


    @for (int i = 0; i < Model.PlayerList.Count; i++) 
    { 
     <tr> 
      <td> 
       @Html.DisplayFor(x => x.PlayerList[i].PlayerName) 
       @Html.HiddenFor(x => x.PlayerList[i].PlayerName) 
      </td> 
      @for (int j = 0; j < Model.PlayerList[i].AttributesList.Count; j++) 
      { 
       <td> 
        @Html.DisplayFor(x => x.PlayerList[i].AttributesList[j].AttributeValue) 
        @Html.HiddenFor(x => x.PlayerList[i].AttributesList[j].AttributeValue) 
       </td> 
      } 
     </tr> 
    } 

</table> 


<input class="btn-danger" type="submit" name="Next >>" value="Next >>" /> 
} 

现在我的问题是,谁来授予答案,因为我可以说大多数答案对我的解决方案非常有帮助

+0

尝试'for'循环而不是'foreach'并使用'@ Html.TextBoxFor(x => x.AttribsPlayerLine [i] .attribValue)''。如果这不起作用,请学习如何使用'EditorFor'并尝试'@ Html.EditorFor(x => x.AttribsPlayerLine [i])'这肯定会起作用。 – mostruash 2015-02-24 19:57:43

+0

嗨mostruash,尝试了你说的,但它没有工作 – Johann 2015-02-24 20:24:47

+0

你的'foreach'循环正在创建多个输入与重复'id'(无效html)和'name'属性(不能绑定到一个集合)。您需要'for'循环或定制'EditorTemplates'作为类型'AttribLine'作为@mostruash所述。 – 2015-02-24 22:56:25

回答

2

以下是您正在尝试做的一个工作示例。这将尽我所能接近你。

让我们先从简单的模型:

public class PlayerRow 
{ 
    public List<AttribLine> AttribsPlayerLine { get; set; } 
} 

public class AttribLine 
{ 
    public string attribName { get; set; } 
    public string attribValue { get; set; } 
} 

注意,重要的是要包括{获得;组; },因此模型联编程序知道它在块上进行绑定。

@using (Html.BeginForm("Calculate", "PlayerRow", FormMethod.Post)) 
{ 
    <table> 
     @*/*Build out header*/*@ 
     <tr> 
      @foreach (AttribLine a in Model.AttribsPlayerLine) 
      { 
       <th>@Html.Label("title", a.attribName)</th> 
      } 
     </tr> 
     @*/* Add row of our player attributes */*@ 
     <tr> 
      @foreach (AttribLine a in Model.AttribsPlayerLine) 
      { 
       using (Html.BeginCollectionItem("AttribsPlayerLine")) 
       { 
        <td> 
         @Html.TextBox("attribValue", a.attribValue) 
         @Html.Hidden("attribName", a.attribName) 
         @* 
          /* Add any more [AttribLine] properties as hidden here */ 
         *@ 
        </td> 
       } 
      } 
     </tr> 
    </table> 
    <input class="btn-danger" type="submit" name="Next >>" value="Next >>" /> 
} 

注意,重要的是在这里,以确保即使属性由用户不可编辑,它需要:

接下来是一个简化视图只在形式()部分寻找作为一个隐藏的元素包含在我们的CollectionItem中,因此模型联编程序可以将它设置在[POST]

希望这会有所帮助。

+0

嗨pnm,它给我一个错误,“无法解析BeginCollectionItem – Johann 2015-02-24 20:28:56

+0

让我跟踪正确的参考 – pnm 2015-02-24 20:32:12

+0

确定添加了Nuget,并没有给出任何问题,但该模型仍然是空的! – Johann 2015-02-24 20:37:14

0

您正确地将模型传递给视图,但是一旦它进入视图,除非您编辑它或在下一个控制器上传递它,否则模型数据将基本上被“清除”,在这种情况下“计算”。

我不知道到底是什么你想传递给控制器​​视图模型的部分,但你可以随时使用这个:

@Html.HiddenFor(model => model.DataYouWantPassedToController) 

现在,你可以随时使用这些物品也来传递数据,如果你要编辑/变更:

@Html.EditorFor(model => model.DataYouWantToEditAndPass)... and so on. 

如果通过不改变或通过它,就像你在做@foreach,该数据将在您的“邮报”的方法中会丢失的数据的简单循环。

+0

没有运气伊莱。我试图添加下面的@ Html.HiddenFor(model => Model.AttribsPlayerLine),当我把一个断点存在数据时,但是当我按下提交按钮时,模型仍然是空的。另外EditorFor没有工作,仍然没有得到任何数据 – Johann 2015-02-24 20:19:16

+0

嗯,这很奇怪。绝对应该给你一个部分填充的数据视图模型。 – 2015-02-24 21:22:11

0

尝试使用@Html.EditorForModel()这样的:

@model Scraper.Facade.PlayerRow 

@using (Html.BeginForm("Calculate", "Home", FormMethod.Post)) 
{ 
    @Html.EditorForModel() 

    <input class="btn-danger" type="submit" name="Next >>" value="Next >>" /> 
} 

创建一个名为PlayerRow.cshtml作为PartialView上的文件夹浏览文件/共享/ EditorTemplates并添加以下代码:

@model Scraper.Facade.PlayerRow 
    <table class="table table-striped table-bordered table-condensed table-responsive table-hover"> 
     @foreach (var player in Model.AttribsPlayerLine) 
     { 
      <thead> 
       <tr class="success"> 
        @foreach (var attrib in player.AttribsPlayerList) 
        { 
         //@Html.DisplayNameFor(x => Model.tytul) 
         <th data-field="@attrib.attribName">@Html.DisplayFor(x => attrib.attribName) </th> 
        } 
       </tr> 
       <tr class="active"> 
        @foreach (var attrib in player.AttribsPlayerList) 
        { 
         <td data-field="@attrib.attribValue">@Html.TextBoxFor(x => attrib.attribValue)</td> 
        } 
       </tr> 
      </thead> 

     } 
    </table> 

我认为这会帮助你。