2010-10-23 33 views
0

我試圖顯示具有類型的一些自定義類的屬性的類。 型號:Post方法中的數據是空的asp.net mvc 2

public class ComplexParent 
{ 
    public SimpleChild First { get; set; } 
    public SimpleChild Second { get; set; } 
} 
public class SimpleChild 
{ 
    public int Id { get; set; } 
    public string ChildName { get; set; } 
    public string ChildDescription { get; set; } 
} 

控制器:

public ActionResult Testify(int id) 
    { 
     ComplexParent par = new ComplexParent(); 
     par.First = new SimpleChild() { Id = id }; 
     par.Second = new SimpleChild() 
     { 
      Id = id + 1, 
      ChildName = "Bob", 
      ChildDescription = "Second" 
     }; 

     return View("Testify", par); 
    } 

    [HttpPost] 
    public ActionResult Testify(ComplexParent pComplexParent) 
    { 

     return View("Testify", pComplexParent); 
    } 

查看:

<% using (Html.BeginForm()) 
    {%> 
<fieldset> 
    <legend>Fields</legend> 
    <%: Html.EditorFor(x => x.First) %> 
    <br /> 
    <%: Html.EditorFor(x => x.Second.ChildName)%> 
    <br/> 
    <br/> 
    <br/> 
    <% Html.RenderPartial("SimpleChild", Model.First); %> 
    <p> 
     <input type="submit" value="Watch me :-)" /> 
    </p> 
</fieldset> 
<% } %> 

當談到獲得它工作得很好,我可以看到所有的數據。但是在後pComplexParent參數是空的(複雜類的兩個屬性都是空值)。大概是我在這裏錯過了一些東西,但無法得到這個工作... 小增加:查看部分,只顯示名稱的編輯器使第二個孩子不爲空,名稱設置爲鮑勃。但我不明白如何使用EditorFor或DisplayFor方法。

更新:感謝達林季米特洛夫,他善待我的代碼,並發現了什麼導致了這個問題。確切的問題是,如果您使用顯示模板,asp.net mvc 2不會發回任何值,如果整個模板沒有任何回發對象爲空。即使您不想編輯數據,我仍然在想如何獲取數據。但是使用編輯器模板可以做到這一點,而且我現在可以用所有對象填充正確的數據

+0

提示 - 使用Fiddler檢查HTTP POST請求以查看正在發送的數據。查看鍵/值對是否與您的Model匹配,因爲這正是MVC嘗試執行的操作(並且明顯是失敗) – RPM1984 2010-10-23 09:24:53

+0

僅使用Request對象進行檢查,目前沒有任何內容。 – 2010-10-23 09:27:58

+0

這就是我的觀點。檢查Fiddler中的'原始HTTP數據包'。如果它不匹配模型,它將爲空。 – RPM1984 2010-10-23 09:28:58

回答

2

您的看法有點混亂。您正在使用編輯器模板以及第一個孩子的部分。表格中包含哪些字段並不十分清楚。我建議你只使用編輯器模板:

型號:

public class ComplexParent 
{ 
    public SimpleChild First { get; set; } 
    public SimpleChild Second { get; set; } 
} 
public class SimpleChild 
{ 
    public int Id { get; set; } 
    public string ChildName { get; set; } 
    public string ChildDescription { get; set; } 
} 

控制器:

[HandleError] 
public class HomeController : Controller 
{ 
    public ActionResult Testify(int id) 
    { 
     var par = new ComplexParent(); 
     par.First = new SimpleChild() { Id = id }; 
     par.Second = new SimpleChild() 
     { 
      Id = id + 1, 
      ChildName = "Bob", 
      ChildDescription = "Second" 
     }; 

     return View(par); 
    } 

    [HttpPost] 
    public ActionResult Testify(ComplexParent pComplexParent) 
    { 
     return View(pComplexParent); 
    } 
} 

觀是SimpleChild

<% using (Html.BeginForm()) { %> 
    <%: Html.EditorFor(x => x.First) %> 
    <%: Html.EditorFor(x => x.Second) %> 
    <input type="submit" value="Watch me :-)" /> 
<% } %> 

編輯模板(~/Views/Home/EditorTemplates/SimpleChild.ascx):

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SomeNs.Models.SimpleChild>" %> 

<%: Html.HiddenFor(x => x.Id) %> 
<%: Html.EditorFor(x => x.ChildName) %> 
<%: Html.EditorFor(x => x.ChildDescription) %> 

現在,如果你想有兩個孩子的性能,包括它時,你既可以指定編輯模板名稱不同的編輯器模板:

<%: Html.EditorFor(x => x.First, "FirstChildEditor") %> 

相當於~/Views/Home/EditorTemplates/FirstChildEditor.ascx或在模型中使用的[UIHint]屬性:

public class ComplexParent 
{ 
    [UIHint("FirstChildEditor")] 
    public SimpleChild First { get; set; } 
    public SimpleChild Second { get; set; } 
} 

我的建議是使用Html.RenderPartial產生輸入字段,因爲他們的名字將被硬編碼的根據您的對象層次結構,d將無法正確綁定。

+0

Darin,謝謝你的幫助。是的,有點混亂的一切。我剛剛創建了一個測試樣本並試圖使其工作。所以看着將不同的方法,如EditorFor和RenderPartial有不同的結果。它不是一個生產代碼,只是我的測試模型,視圖和控制器。 – 2010-10-23 10:22:59