English 中文(简体)
ASP.Net MVC隐藏字段未按预期工作
原标题:ASP.Net MVC Hidden field not working as expected

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

我传递一个列表<>;进入视图。在视图中,我遍历列表,并构建一个表。在表中,我需要添加隐藏字段,这样我就可以在帖子中重新获得值。

然而,我发现当我使用Html.Hiddden时,字段不会被渲染。在视图源中,没有提及字段。。。

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

最佳回答

就我个人而言,我会使用编辑器模板:

<% 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; }
}
问题回答

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

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

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

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

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





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签