我在一個視圖上有「重置密碼」和「用戶配置文件信息更新」部分。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);
}
請張貼一些代碼:) – StuartLC