【问题标题】:ASP.Net MVC Hidden field not working as expectedASP.Net MVC 隐藏字段未按预期工作
【发布时间】:2011-02-17 03:38:28
【问题描述】:

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

我将一个 List 传递给一个视图。在视图中,我“foreach”了列表,并构建了一个表。在表格中,我需要添加隐藏字段,以便我可以在帖子中获取值。

但是,我发现当我使用 Html.Hidden 时,不会呈现该字段。在查看源代码中,没有提到字段...

        <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 事件的控制器中读取它。

【问题讨论】:

  • 你能检查一下 Model.TransactionSplitLines.Length>0 吗?你还看到 line.Category 渲染了吗?

标签: c# asp.net-mvc


【解决方案1】:

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

<% 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):

<%@ 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>

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

[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 如下所示:

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

和:

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

【讨论】:

  • 谢谢!打算试试这个。我将 TransactionSplitLines 作为 LIST 而不是 IEnumerable。这可以吗,还是必须 IENumerable 才能工作?
  • @cdotlister,最好始终使用层次结构中最高的类/接口。因此,例如,如果您只想枚举似乎是这种情况,请使用IEnumerable&lt;T&gt;
  • 我的问题是我需要添加项目 - foreach (var c in model.CategoryIds) { reply.TransactionSplitLines.Add(new TransactionSplitLine {Amount = "100", Category = "Test", SubCategory = "测试更多", CategoryId=int.Parse(c)}); }
  • @cdotlister,这是控制器而不是视图的责任。如果您需要使用 javascript 动态添加项目,那是另一回事:blog.stevensanderson.com/2010/01/28/…
  • 对不起,达林 - 我不清楚 - 感谢您到目前为止的耐心......该代码在控制器中。基本上,我(目前)传入一个 List,并基于该列表构建一个表。在表单中,用户选择新的类别、子类别和数量,然后单击“添加”按钮。表单返回到控制器,我的想法是我将表单值添加到 List,然后返回带有添加到表中的表单值的视图。这可以多次完成,因此每次都会将表单值添加到 List (因此,表格)。
【解决方案2】:

我相信这会生成具有相同 ID (CategoryID) 的多个字段,您是否尝试过不使用 Html.Helper 并简单地将其替换为:

<input type='hidden' value='<%= line.CategoryId.ToString()%>' />

但是 - 如果您需要通过某种类型的 ID 获取它们 - 您可以在其中添加一个类并使用 jQuery 来提取您的所有值:

<input class='CategoryId' type='hidden' value='<%=line.CategoryId.ToString()%>'/>

然后只需使用$('.CategoryId').each() 函数来填充您的列表并将其发布到您的控制器。

【讨论】:

    猜你喜欢
    • 2010-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2022-08-03
    • 2019-12-25
    • 1970-01-01
    • 2013-08-21
    相关资源
    最近更新 更多