ASP.Net MVC隐藏字段无法按预期方式工作 [英] ASP.Net MVC Hidden field not working as expected

查看:48
本文介绍了ASP.Net MVC隐藏字段无法按预期方式工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能有一个奇怪的要求,但是我认为实现它很容易.

I have maybe a strange requirement, but I thought implementing it would be easy.

我将一个列表<>传递给视图.在视图中,我遍历"列表,并构建一个表.在表格中,我需要添加隐藏字段,以便可以将值重新返回到帖子中.

I pass a List<> into a View. Within the view, I 'foreach' though the list, and build a Table. Within the table, I need to add hidden fields, so that I can get the values back on the post.

但是,我发现当我使用Html.Hidden时,不会呈现该字段.在视图源中,没有提及字段...

However, I am finding that when I use Html.Hidden, the field isn't rendered. In the view source, there is no mention of the fields...

        <table width="600">
        <tr class="headerRow">
            <td>
                Category
            </td>
            <td>
                Sub Category
            </td>
            <td>
                Amount
            </td>
        </tr>
        <% 
            if (Model.TransactionSplitLines != null)
            {
                foreach (var line in Model.TransactionSplitLines)
                {%>

        <tr>
            <td>
                <%=line.Category%>                
                <%=Html.Hidden("CategoryId", line.CategoryId.ToString()) %>

            </td>
            <td>
                <%=line.SubCategory%>
            </td>
            <td>
                <%=line.Amount%>
            </td>
        </tr>
        <%
            }
        } %>
    </table>
</div>
<input type="submit" value="Save" />

那时我希望做的是拥有一个CategoryId数组,我可以在Action事件中的控制器中读取该数组.

When I am then hoping to do is have an array of CategoryId, which I can read within the controller on the Action event.

推荐答案

我个人将为此使用编辑器模板:

Personally I would use an editor template for this:

<% using (Html.BeginForm()) { %>
    <table width="600">
        <thead>
            <tr class="headerRow">
                <th>Category</th>
                <th>Sub Category</th>
                <th>Amount</th>
            </tr>
        </thead>
        <tbody>
            <%= Html.EditorFor(x => x.TransactionSplitLines) %>
        </tbody>
    </table>
    <input type="submit" value="Save" />
<% } %>

,然后有一个相应的编辑器模板(〜/Views/Shared/EditorTemplates/TransactionSplitLine.ascx ):

and then have a corresponding editor template (~/Views/Shared/EditorTemplates/TransactionSplitLine.ascx):

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<AppName.Model.TransactionSplitLine>" %>
<tr>
    <td>
        <%= Html.DisplayFor(x => x.Category) %>
        <%= Html.HiddenFor(x => x.CategoryId) %>
    </td>
    <td>
        <%= Html.DisplayFor(x => x.SubCategory) %>
    </td>
    <td>
        <%= Html.DisplayFor(x => x.Amount) %>
    </td>
</tr>

现在,不仅您的视图更具可读性,而且编辑器模板将负责为隐藏字段生成专有名称,以便您可以将模型绑定回去:

Now not only that your view are much more readable but editor templates will take care of generating proper names for your hidden fields so that you can bind your model back:

[HttpPost]
public ActionResult Index(SomeViewModel model)
{
    // TODO: model.TransactionSplitLines should be properly bound 
    // at least the CategoryId property because it's the only one 
    // you are sending in the post
    ...
}

其中SomeViewModel如下所示:

where SomeViewModel looks like this:

public class SomeViewModel 
{
    public IEnumerable<TransactionSplitLine> TransactionSplitLines { get; set; }
}

和:

public class TransactionSplitLine
{
    public string CategoryId { get; set; }
}

这篇关于ASP.Net MVC隐藏字段无法按预期方式工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆