Html.BeginForm()没有传递模型

时间:2015-02-24 19:48:32

标签: c# asp.net-mvc razor html-helper html.beginform

我有以下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; 

}

更新

大家好,我改变了我的应用程序的逻辑,基本上有2个列表,其中包含Header属性,以及所有玩家的属性,所以现在只有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 >>" />
}

现在我的问题是,谁给予答案,因为我可以说大多数答案真的有助于我的解决方案

3 个答案:

答案 0 :(得分:2)

这是您正在尝试做的一个工作示例。这就像我能得到你一样接近。

让我们从简化模型开始:

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

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

请注意,包括{get;组;在每个模型属性上,因此模型绑定器知道它在绑定块上。

接下来是仅查看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]上设置它

希望这有帮助。

答案 1 :(得分:0)

您正确地将模型传递给视图,但一旦到达视图,模型数据将基本上被清除&#39;除非你编辑它或在下一个控制器上传递它,在这种情况下&#34;计算&#34;。

我不确定你想要的视图模型的哪些部分传递给控制器​​,但你总能使用它:

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

现在,如果要编辑/更改,您也可以使用这些项目来传递数据:

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

如果您简单地循环访问数据而不更改或传递数据,就像在@foreach中一样,数据将在您的“发布”过程中丢失。方法

答案 2 :(得分: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 >>" />
}

在文件夹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>

我认为这会对你有所帮助。