2012-08-31 20 views
1

我在一個視圖上有「重置密碼」和「用戶配置文件信息更新」部分。MVC 3一個視圖中的兩個模型和操作方法超載

我有兩個孩子機型父模型:ResetPwdModel和UserInfoModel。

對於這兩種重置密碼提交併保存資料信息提交,我想要的結果URL看起來相同(與當前URL),所以用戶不會混淆。

在努力實現這一點,我重載與一個接受ResetPwdModel和另一個接受UserInfoModel作用的方法。但是,我得到錯誤信息The current request for action 'profile' on controller type 'XXController' is ambiguous...

有沒有解決這個優雅的方式?我的目標是能夠在1個視圖中使用兩個模型,並使後期網址與當前網址相同。

型號

public class ProfileParentModel 
{ 
    public ProfileResetPwdModel ResetPwd { get; set; } 
    public ProfileUserInfoModel UserInfo { get; set; } 
} 

public class ProfileResetPwdModel 
{ 
    [Required] 
    [DataType(DataType.Password)] 
    [Display(Name = "Profile_lbl_OldPassword", ResourceType = typeof(Resource))] 
    public string OldPassword { get; set; } 

    [Required] 
    [DataType(DataType.Password)] 
    [Display(Name = "Profile_lbl_NewPassword", ResourceType = typeof(Resource))] 
    public string NewPassword { get; set; } 

    [Compare("NewPassword", ErrorMessageResourceName = "Profile_Error_NotMatch", ErrorMessageResourceType = typeof(Resource))] 
    [Required] 
    [DataType(DataType.Password)] 
    [Display(Name = "Profile_lbl_ConfirmNewPassword", ResourceType = typeof(Resource))] 
    public string ConfirmNewPassword { get; set; } 
} 

public class ProfileUserInfoModel 
{ 
    [Display(Name = "Profile_lbl_FirstName", ResourceType = typeof(Resource))] 
    public string FirstName { get; set; } 

    [Display(Name = "Profile_lbl_LastName", ResourceType = typeof(Resource))] 
    public string LastName { get; set; } 

    [Display(Name = "Profile_lbl_WorkTitle", ResourceType = typeof(Resource))] 
    public string WorkTitle { get; set; } 

    [Display(Name = "Profile_lbl_CompanyName", ResourceType = typeof(Resource))] 
    public string CompanyName { get; set; } 

    [Display(Name = "Profile_lbl_CompanyAddress", ResourceType = typeof(Resource))] 
    public string CompanyAddress { get; set; } 
} 

查看

@{ 
    var passwordHtml = Html.HtmlHelperFor(Model.ResetPwd); 
    var profileHtml = Html.HtmlHelperFor(Model.UserInfo); 
} 
@using (passwordHtml.BeginForm()) 
{ 
    //HTML goes here... 
} 
@using (profileHtml.BeginForm()) 
{ 
    //HTML goes here... 
} 

控制器

 [ActionName("Profile")] 
    [HttpPost] 
    [Authorize] 
    public ActionResult ResetPassword(ProfileResetPwdModel model) 
    { 

     return View("Profile", parentModel); 
    } 

    [ActionName("Profile")] 
    [HttpPost] 
    [Authorize] 
    public ActionResult SaveUserInfo(ProfileUserInfoModel model) 
    { 
     return View("Profile", parentModel); 
    } 
+0

請張貼一些代碼:) – StuartLC

回答

4

你的動作方法名是不相關到你想要的響應URL。

首先,您是否使用兩個單獨的Html表單提交數據?我會說這是最好的方法,並指出每一個不同的行動。然後,您的操作可以重定向到您想要的任何操作。

接下來,您將無法將子模型指定爲操作的參數,因爲框架將無法自動計算出屬性(因爲您使用父模型開始),但這實際上取決於如何您可以定義表單域的名稱。

如果您使用的是Html.TextBoxFor方法的例子。那麼這將給出'parent.childproperty'的字段名稱。在這種情況下,你應該使用父模型作爲兩個動作參數:

public ActionResult ResetPassword(ParentModel model)... 
public ActionResult UpdateInfo(ParentModel model)... 

如果你很高興自己手動設置字段名稱,以匹配子類的屬性,那麼你可以使用子模型作爲parameterss :

public ActionResult ResetPassword(ResetPwdModel model)... 
public ActionResult UpdateInfo(UserInfoModel model)... 

與字段設置,如:

@Html.TextBox("NewPassword", Model.ResetPwdModel.NewPassword) 

,他們會正確映射。

處理每個動作後僅添加以下底:

return RedirectToAction("Profile"); 
+0

是的我使用兩個單獨的HTML表單提交。我在MVCUtil中使用HTML Helper – Laguna

+0

現在,如果我發佈到ResetPassword,我的返回URL以「/ ResetPassword」結尾,對於updateInfo也是如此......我如何讓URL保持/Profile?...什麼是我想念...謝謝! – Laguna

+0

@Laguna:正如我所說的,你必須返回'RedirectToAction'並傳遞你想要顯示的動作名稱。隨着你的更新問題,你應該通過「簡介」 – musefan

0

的視圖()控制器has overload的方法具有過載接受的視圖作爲參數名稱。因此,您可以在不同的操作中使用相同的視圖。

+0

雖然這不會改變網址 – musefan

1

我建議你在每個動作結束時做一個重定向。

public ViewResult Profile() 
{ 
    ...get the profile info of the user from db 
    return View(profile); 
} 

[HttpPost] 
public ActionResult ResetPassword(ProfileResetPwdModel model) 
{ 
    ...reset the password 
    return RedirectToAction("Profile"); 
} 

public ActionResult SaveUserInfo(ProfileUserInfoModel model) 
{ 
    ...update the profile 
    return RedirectToAction("Profile"); 
} 

通過這種方式,用戶不會感到困惑。

相關問題