2012-06-21 60 views
0

我是新來的整個MVC 3風格的編碼。我遇到了一個問題,但首先這是我如何佈置我的網站。MVC3一個視圖 - 多個部分視圖 - 每個部分視圖一個模型

_Layout.cshtml包含

 @if (Request.IsAuthenticated) 
     { 
      <div class="span2"> 
       @Html.Partial("_NavigationPartial") 
      </div> 
      <div class="span10"> 
       @RenderBody() 
      </div> 
     } 
     else 
     { 
      @RenderBody() 
     } 

@RenderBody會顯示我的Profile.cshtml文件,該文件包含以下內容:

@{ 
    ViewBag.Title = "My Profile"; 
} 
<div class="row-fluid well"> 
    <div class="page-header"> 
     <h1> 
      Profile</h1> 
    </div> 
    @{ 
     Html.RenderPartial("ChangePersonalInformationPartial"); 
     Html.RenderPartial("ChangePasswordPartial"); 
     } 
</div> 

正如你看到的,我有兩個局部模板(之一更改個人信息,另一個更改密碼)。

這些Partials中的每一個都使用它自己的Model(ChangePersonalInformationModelChangePasswordModel)。

我的問題來了,當我點擊提交我的ChangePasswordPartial,它重新加載_Layout.cshtml頁面,但這次只加載ChangePasswordPartial.cshtml。我需要它來加載Profile.cshtml。但是,如果我繼續前進,在我AccountController.cs改變return View();return View("Profile");我得到一個錯誤說:

傳遞到字典的模型產品類型 「PROJECT.Models.ChangePasswordModel」,但這需要字典一個 型號項目'PROJECT.Models.ChangePersonalInformationModel'。

我怎樣才能解決這個問題?

謝謝!

回答

2

基本上,您必須在保存密碼信息後重定向到ChangePassword操作中的配置文件操作。

UPDATE:

首先,你應該有一個共同的模型說ProfileModel它包裝起來ChangePasswordModelChangePersonalInformationModel

因此,下面是顯示用於查看和編輯的配置文件信息的操作。

// this action will returns a views that displays profile info 
public ViewResult Profile(string username) 
{ 
    ProfileModel model = .. get the profile from database based on username 

    return View(model); 
} 

// this action will returns the profile info for editing or adding a new profile 
public ViewResult EditProfile(string username) 
{ 
    .. if the profile already exists get from database 
    ProfileModel model = 

    .. if this is a new profile create an empty model 
    ProfileModel model = new ProfileModel(); 
    model.ChangePasswordModel = new ChangePasswordModel(); 
    model.ChangePersonalInformationModel = new ChangePersonalInformationModel(); 

    return View(model); 
} 

EditProfile.cshtml會是這樣

@model Models.ProfileModel 

... 
@{ 
    Html.RenderPartial("ChangePersonalInformationPartial", 
            Model.ChangePersonalInformationModel); 
    Html.RenderPartial("ChangePasswordPartial", Model.ChangePasswordModel); 
} 
... 

這將是你ChangePassword行動

[HttpPost] 
public ActionResult ChangePassword(ChangePasswordModel model) 
{ 
    if(ModelState.IsValid) 
    { 
    // save the ChangePasswordModel to database and display the profile info 
    // or even you can redirect to EditProfile for more editing 
    return RedirectToAction("Profile"); 
    }  

    .. there are validation errors so get the complete profile model from database 
    .. the ChangePasswordModel form will be filled by the details entered in the form 
    .. and not from the db details this will be taken care by the framework itself. 
    ProfileModel model = 
    return View("EditProfile", model);  
} 
+0

謝謝,一個問題,我怎麼會在傳遞一個值我的看法說「嗨,密碼已成功更改?」。 'RedirectToAction'參數下的'routeValues'會訣竅嗎? –

+0

@WesleyPattison通常你使用TempData來做類似的事情。您可以存儲只能用於下一個請求的字符串(或其他對象)。 – Dismissile

+0

還有一個問題,這是非常嚴重的。我的'ChangePasswordModel'錯誤(就像無效的密碼,等等......)我怎麼傳遞通過,所以我'@ Html.Validation *'將工作? –

相關問題