以IDictionary< T,T>结尾的和ModelBinders,ASP.NET MVC 2.0 [英] Wits End with IDictionary<T,T> and ModelBinders, ASP.NET MVC 2.0

查看:75
本文介绍了以IDictionary< T,T>结尾的和ModelBinders,ASP.NET MVC 2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我并没有尝试做任何超级复杂的事情,我已经搜索,搜索并漫游了我能找到的每个博客和论坛.我要扯掉我的头发,因为没有人能直接显示.

Okay, I'm not trying to do anything super complex, I have searched, and searched, and roamed every blog and forum I can find. I am about to tear my hair out because no one just outright shows it working.

我想在我的模型中使用IDictionary.没关系的原因,这无关紧要.我只想做一些非常简单的事情,并了解发生了什么.

I want to use an IDictionary in my Model. Nevermind the reason why, that's irrelevant. I just want to do something very simple, and understand what is going on.

有人可以帮我弄清楚我应该在View中添加的HTML,以使其正常工作吗?请?我已经尝试过Html.TextBoxFor,它在回发时返回null.我尝试使用字典项的索引进行手动尝试,但返回的结果为null.我真的很沮丧,我一直看到的是重定向到无法回答问题的博客.

Can someone please help me figure out the HTML I should be putting in the View to make this work right? Please? I have tried Html.TextBoxFor and it comes back null on the postback. I have tried manually using the index of the dictionary item, and it comes back null. I am really getting frustrated, and all I keep seeing is redirection to blogs that don't answer the question.

    public ActionResult DictionaryView()
    {
        var model = new Dictionary<string, int>
        {
            { "First", 0 },
            { "Second", 0 },
            { "Third", 0 },
            { "Fourth", 0 }
        };

        return View(model);
    }

    [HttpPost]
    public ActionResult DictionaryView(Dictionary<string, int> dictionary)
    {
        return null;
    }

HTML

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<System.Collections.Generic.IDictionary<string,int>>" %>

<asp:Content ContentPlaceHolderID="MainContent" runat="server">

    <h2>Dictionary</h2>
<% using (Html.BeginForm()) {%>
        <%: Html.ValidationSummary(true) %>
    <% for (int i = 0; i < Model.Count; i++) { %>
        **// This is there I am stuck!** 
    <% } %>
            <p>
                <input type="submit" value="Submit" />
            </p>
    <% } %>
</asp:Content>

推荐答案

http://www. hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

示例

<%
    int i = 0;
    foreach (KeyValuePair<string, int> pair in Model)
    { %>
    <p>Key: <%= Html.TextBox(String.Format("[{0}].key", i), pair.Key)%></p>
    <p>Value: <%= Html.TextBox(String.Format("[{0}].value", i), pair.Value)%></p>
<%
    i++;
    }
%>

这应该有效

这篇关于以IDictionary&lt; T,T&gt;结尾的和ModelBinders,ASP.NET MVC 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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