It is tempting to try overloading actions when one has partial views with multiple forms. But it is the wrong strategy. The trick to embedding partial views with multiple forms is to
- Name the action according to the view, as you normally would, whether full or partial
Each form specifies its action by utilizing:
using (Html.BeginForm("Action", "Controller"))
All actions return the parent view
In the following example, there are three views, one full and two partial. The first view contains the second two:
- ManageUser.aspx
- ManageUserPassword.ascx
- ManageUserRoles.ascx
The three associated actions are shown below. Note that each action is named after its corresponding view; however, they each return to the parent:
return View("ManageUser", user);
Good luck!
--Brett
// **************************************
// URL: /Admin/ManageUser/id
// **************************************
[Authorize]
public ActionResult ManageUser(string id)
{
MembershipUser user = MembershipService.GetUser(id, false);
ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
return View("ManageUser", user);
}
[Authorize]
[HttpPost]
public ActionResult ManageUserPassword(AdminChangePasswordModel model)
{
if (ModelState.IsValid)
{
if (ChangePasswordLogic())
{
TempData["Confirmation"] = "Password successfully updated.";
}
else
{
ModelState.AddModelError("", "Change password failed.");
}
}
// Return the parent view
MembershipUser user = MembershipService.GetUser(model.UserName, false);
ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
return View("ManageUser", user);
}
[Authorize]
[HttpPost]
public ActionResult ManageUserRoles(AdminRolesModel model)
{
if (ModelState.IsValid)
{
try
{
ChangeRolesLogic();
TempData["Confirmation"] = "Roles successfully updated.";
}
catch (Exception)
{
ModelState.AddModelError("", "Role update failed.");
}
}
// Return the parent view
MembershipUser user = MembershipService.GetUser(model.UserName, false);
ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
return View("ManageUser", user);
}