2010-07-12 2 views
1

DefaultModelBinder을 사용하는 다음보기 모델과 동작을 감안할 때 사전을 무시하고 다른 모든 속성을 올바르게 바인딩하는 것처럼 보입니다. 내가 여기서 뭔가를 놓치고 있니? MVC 소스 코드를 보면 이것은 합법적 인 것처럼 보입니다.ASP.NET MVC 모델 바인더가 사전과 작동하지 않습니다.

감사

public class SomeViewModel 
{ 
    public SomeViewModel() 
    { 
     SomeDictionary = new Dictionary<string, object>(); 
    } 

    public string SomeString { get; set; } 
    public IDictionary<string, object> SomeDictionary { get; set; } 
} 

[HttpPost] 
public ActionResult MyAction(SomeViewModel someViewModel) 
{ 
    //someViewModel.SomeString binds correctly 
    //someViewModel.SomeDictionary is null 
} 

<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<SomeViewModel>" MasterPageFile="~/Views/Shared/Site.Master" %> 
<asp:Content runat="server" ID="Content2" ContentPlaceHolderID="MainContent"> 
<% using (Html.BeginForm("MyAction", "MyController")) {%> 

    <%= Html.EditorFor(m => m.SomeString) %> 
    <%= Html.EditorFor(m => m.SomeDictionary["somevalue"]) %> 

    <input type="submit" value="Go" /> 
<%} %> 
</asp:Content> 

그리고 참조

, HTML 출력은 다음과 같습니다

<input class="text-box single-line" id="SomeString" name="SomeString" type="text" value="" /> 
<input class="text-box single-line" id="Somedictionary_somevalue_" name="SomeDictionary[somevalue]" type="text" value="" /> 

편집 : 위 아래 지적했다, 그러나 나는이 레이아웃을 선호으로 작동하지 않습니다 다음과 같은 빠른 해킹 내 요구 작동, 그것은 일부는 OFC를 정리해야

someViewModel.SomeDictionary = (from object key in Request.Form.Keys 
           where key.ToString().StartsWith("SomeDictionary[") 
           select new 
           { 
            Key = key.ToString().Replace("SomeDictionary[", string.Empty).Replace("]", string.Empty), 
            Value = (object)Request.Form[key.ToString()] 
           }).ToDictionary(arg => arg.Key, arg1 => arg1.Value); 

... 단지 게시 한 후이 전화 ourse :)

답변

6

this post에서 사전을 바인딩하는 방법을 살펴보십시오. 강력하게 입력 된 EditorFor 도우미를 사용하면이 작업을 수행 할 수 없으며 수동으로 필드를 생성해야합니다.

+0

아하, 이제 알겠습니다. 감사합니다! 나는이 레이아웃을 정말 좋아하지 않지만, 나의 요구에 따라 작동하는 매우 빠른 해킹을 가지고있다. – amarsuperstar