首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Html.BeginForm()没有传递模型

Html.BeginForm()没有传递模型
EN

Stack Overflow用户
提问于 2015-02-24 19:48:32
回答 3查看 33.8K关注 0票数 2

我有以下cshtml表单:

代码语言:javascript
复制
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中获得模型

代码语言:javascript
复制
[HttpPost]
public ActionResult Calculate(PlayerRow model)
{
    GenericHelper _genericHelper = new GenericHelper();
    return View();
}

但是,PlayerRow模型始终为空。

我做错了什么?

这是我的模型定义

代码语言:javascript
复制
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; 

}

更新

大家好,我稍微改变了我的应用程序的逻辑,基本上有两个列表,其中有标题属性,以及所有玩家的属性,所以现在只有两个类,我像这样修改了cshtml,它现在正在工作:-

代码语言:javascript
复制
@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 >>" />
}

现在我的问题是,谁来奖励答案,因为我可以说,大多数答案都对我的解决方案很有帮助。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-02-24 20:24:02

下面是你想要做的事情的一个例子。我尽我所能找到你。

让我们从简化模型开始:

代码语言:javascript
复制
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()部分:

代码语言:javascript
复制
@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上设置它。

希望这能有所帮助。

票数 3
EN

Stack Overflow用户

发布于 2015-02-24 20:04:54

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

我不确定要将视图模型的哪些部分传递给控制器,但您可以始终使用以下方法:

代码语言:javascript
复制
  @Html.HiddenFor(model => model.DataYouWantPassedToController)

现在,如果要编辑/更改,可以始终使用这些项传递数据:

代码语言:javascript
复制
  @Html.EditorFor(model => model.DataYouWantToEditAndPass)... and so on.

如果您在不更改或传递数据的情况下对数据进行简单循环,就像在@foreach中所做的那样,数据将在“Post”方法中丢失。

票数 0
EN

Stack Overflow用户

发布于 2015-02-24 20:55:08

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

代码语言:javascript
复制
@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 /Shared/EditorTemplate文件夹上创建一个名为PartialView的文件,并添加以下代码:

代码语言:javascript
复制
    @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>

我认为这会对你有帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28704925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档