我有以下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 >>" />
}
现在我的问题是,谁来授予答案,因为我可以说大多数答案对我的解决方案非常有帮助
尝试'for'循环而不是'foreach'并使用'@ Html.TextBoxFor(x => x.AttribsPlayerLine [i] .attribValue)''。如果这不起作用,请学习如何使用'EditorFor'并尝试'@ Html.EditorFor(x => x.AttribsPlayerLine [i])'这肯定会起作用。 – mostruash 2015-02-24 19:57:43
嗨mostruash,尝试了你说的,但它没有工作 – Johann 2015-02-24 20:24:47
你的'foreach'循环正在创建多个输入与重复'id'(无效html)和'name'属性(不能绑定到一个集合)。您需要'for'循环或定制'EditorTemplates'作为类型'AttribLine'作为@mostruash所述。 – 2015-02-24 22:56:25