我有以下cshtml表单:
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;
}
更新
嗨,大家好,我改变了我的应用程序的逻辑,基本上有两个列表,其中有Header Attributes,和所有玩家的Attributes,所以现在只有两个类,我把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 >>" />
}
现在我的问题是,谁来奖励答案,因为我可以说,大多数答案对我的解决方案
下面是一个您正在尝试做的工作示例。
让我们从简化的模型开始:
public class PlayerRow
{
public List<AttribLine> AttribsPlayerLine { get; set; }
}
public class AttribLine
{
public string attribName { get; set; }
public string attribValue { get; set; }
}
请注意,在每个模型属性上包含{get;set;}非常重要,这样模型绑定器就知道它在块上进行绑定。
接下来是一个只看form()部分的简化视图:
@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]上
希望这能有所帮助。
您正确地将模型传递到视图,但一旦它到达视图,模型数据基本上将被"清除",除非您编辑它或将它传递到下一个控制器,在这种情况下为"计算"。
我不确定你想把视图模型的哪些部分传递给控制器,但你可以一直使用这个:
@Html.HiddenFor(model => model.DataYouWantPassedToController)
现在,如果你想编辑/更改,你也可以随时使用这些项目来传递数据:
@Html.EditorFor(model => model.DataYouWantToEditAndPass)... and so on.
如果像在@foreach中那样,在不更改或传递数据的情况下简单地循环数据,则数据将在"Post"方法中丢失。
尝试使用这样的@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 >>" />
}
在Views/Shared/EditorTemplates文件夹中创建一个名为PlayerRow.cshtml
的文件作为PartialView,并添加以下代码:
@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>
我认为这会对你有所帮助。