2013-05-02 101 views
1

我有一個註冊主視圖,顯示兩種不同類型的地址1.家庭地址2.郵寄地址管窺單強類型的不同類型的兩個相似的類

public class RegisterModel 
    {    
     public AddressModel HomeAddress { get; set; } 
     public AddressModel MailAddress { get; set; } 
    } 

public class AddressModel 
{ 
    public string Street1 { get; set; } 
    public string Street2 { get; set; } 
    public string State { get; set; } 
    public string City { get; set; } 
} 

我的主要註冊查看強烈鍵入的RegisterModel如下

@model MyNamespace.Models.RegisterModel 
@{ 
    Layout = "~/Views/_Layout.cshtml"; 
} 
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "myForm" })) 
{ 
    <div id="form"> 
    @Html.Action("MyAddressPartial") 
    @Html.Action("MyAddressPartial") 
    </div> 
} 

MyAddressPartialView如下: -

@model MyNamespace.Models.AddressModel 
@{ 
    Layout = "~/Views/_Layout.cshtml"; 
} 
<div id="Address"> 
    @Html.TextBoxFor(m=>m.Street1 ,new { @id="Street1 "})  
    @Html.TextBoxFor(m=>m.Street2,new { @id="Street2"}) 
    @Html.TextBoxFor(m=>m.State ,new { @id="State "}) 
    @Html.TextBoxFor(m=>m.City,new { @id="City"}) 
</div> 

我RegisterController: -

// Have to instantiate the strongly Typed partial view when my form first loads 
// and then pass it as parameter to "Register" post action method. 
// As you can see the @Html.Action("MyAddressPartial") above in main  
// Register View calls this. 
public ActionResult MyAddressPartial() 
{ 
    return PartialView("MyAddressPartialView", new AddressModel()); 
} 

提交我的主要形式,以低於同一註冊控制器提到的操作方法。

[HttpPost] 
public ActionResult Register(RegisterModel model, 
          AddressModel homeAddress, 
          AddressModel mailingAddress) 
{ 
     //I want to access homeAddress and mailingAddress contents which should 
     //be different, but as if now it comes same. 
} 

我不想爲MailingAddress和HomeAddress創建一個單獨的類。如果我這樣做,那麼我將不得不爲每個地址創建兩個單獨的強類型局部視圖。

有關如何重用類和部分視圖並使其動態化並在Action Method Post中讀取其單獨值的任何想法。

編輯1回覆斯科特 - 帕斯科 -

在DisplayTemplates文件夾,我添加以下AddressModel.cshtml

<div> 
     @Html.DisplayFor(m => m.Street1); 
     @Html.DisplayFor(m => m.Street2); 
     @Html.DisplayFor(m => m.State); 
     @Html.DisplayFor(m => m.City);    
</div> 

而且在EditorTemplate文件夾,我添加以下AddressModel.cshtml但EditorFor

<div> 
    @Html.EditorFor(m => m.Street1); 
    @Html.EditorFor(m => m.Street2); 
    @Html.EditorFor(m => m.State); 
    @Html.EditorFor(m => m.City);    
    </div> 

現在我該如何在RegisterView中使用它們,以及如何在控制器的pos中讀取值行動方法?還有什麼需要修改?我已經添加了上面的幾乎全部代碼。我非常喜歡MVC。

回答

2

這樣做的典型ASP.NET MVC方法是爲您的自定義類型使用EditorTemplates和DisplayTemplates。

In〜/ Views/Shared,創建兩個文件夾,DisplayTemplatesEditorTemplates。 在DisplayTemplates文件夾中,使用您的模型的名稱(地址模型)創建一個局部視圖,並創建一個DisplayFor模板。

在EditorTemplates文件夾中創建名爲AddressModel.cshtml的另一個局部視圖並創建EditorFor模板。

MVC會自動使用您的模板併爲您提供所需的數據。

+0

請看看我的編輯1回覆scott-pascoe.Please讓我知道還有什麼需要做的。 – user2232861 2013-05-02 20:27:40

+0

@ marcind,請看看這個嗎? – user2232861 2013-05-02 20:33:55

+0

謝謝..我現在意識到,EditorFor幫助。 – user2232861 2013-05-06 18:05:58

0
在你看來

使用@ Html.EditorFor(或@ Html.DisplayFor,用於顯示):

@model MyNamespace.Models.RegisterModel 
@{ 
    Layout = "~/Views/_Layout.cshtml"; 
} 
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "myForm" })) 
{ 
    <div id="form"> 
     @Html.EditorFor(m => m.HomeAddress) 
     @Html.EditorFor(m => MailAddress) 
    </div> 
} 

你不會需要有部分單獨的控制器動作,只是填充在地址RegisterModel之前在你的控制器。像這樣:

[HttpGet] 
public ActionResult Register() // this will be the page people see first 
{ 
    var model = new RegisterModel(); 
    return View(model); // assuming your view is called Register.cshtml 
} 


[HttpPost] 
public ActionResult Register(RegisterModel model){ 
    DosomethingWithHomeAddress(model.HomeAddress); 
    DosomethingWithMailAddress(model.MailAddress); 
    model.IsSaved = true; // some way to let the user knwo that save was successful; 
          // if this is true, display a paragraph on the view 
    return View(model); 
} 
+0

我讚賞你的答案。我很愚蠢,因爲我告訴我在mvc中天真。我沒有得到這部分model.HomeAddress = GetAddress(...調用一個方法來獲取家庭地址..);我只想將註冊表單中的數據傳遞迴HttpPost方法,那麼我會在GetAddress中寫入HttpGet的哪些代碼? – user2232861 2013-05-02 22:15:35

+0

對不起,我的錯誤,我想你可能有地址閱讀從某個地方,如果你編輯它。如果你不這樣做,你只需要創建模型。 :) – gabnaim 2013-05-02 22:18:32

+0

,我仍然沒有得到它,當你說我只需要創建模型。你能修改你的答案嗎? – user2232861 2013-05-02 22:21:51