Currently I'm using a standard Manage page which looks like:
public ActionResult Manage(string type = "")
{
ViewBag.Type = type;
switch (type)
{
case "EmailAddress":
ViewBag.EmailAddress = lol.UserProfiles.Find((int)Membership.GetUser().ProviderUserKey).EmailAddress;
break;
case "Password":
break;
default:
break;
}
//ViewBag.ReturnUrl = Url.Action("Manage/" + type);
return View();
}
Now my Manage Model page looks like:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Manage(ManageViewModel model)
{
if (ModelState.IsValid)
{
if (model.EmailAddressModel != null)
{
try
{
int userID = (int)Membership.GetUser().ProviderUserKey;
var User = lol.UserProfiles.First(f => f.UserId == userID);
User.EmailAddress = model.EmailAddressModel.EmailAddress;
int saveStatus = lol.SaveChanges();
if (saveStatus == 1)
{
ViewBag.StatusMessage = MessagesEnum.ChangeEmailSuccess;
return RedirectToAction("Manage/EmailAddress", "Account");
}
else
{
ModelState.AddModelError("", MessagesEnum.ChangeEmailFailed);
}
}
catch { }
}
else
{
// ChangePassword will throw an exception rather than return false in certain failure scenarios.
bool changePasswordSucceeded;
try
{
changePasswordSucceeded = WebSecurity.ChangePassword(User.Identity.Name, model.PasswordModel.OldPassword, model.PasswordModel.NewPassword);
}
catch (Exception)
{
changePasswordSucceeded = false;
}
if (changePasswordSucceeded)
{
ViewBag.StatusMessage = MessagesEnum.ChangePasswordSuccess;
return RedirectToAction("Manage/Password", "Account");
}
else
{
ModelState.AddModelError("", MessagesEnum.ChangePasswordFailed);
}
}
}
else
{
}
return View(model);
}
My ManageViewModel class:
public class ManageViewModel
{
public LocalPasswordModel PasswordModel { get; set; }
public LocalEmailAddressModel EmailAddressModel { get; set; }
}
What I was trying to do is make it where if they access multiple pages on Manage such as /Manage/Password and /Manage/EmailAddress and according to those links, it will post a different page. Now first of all, is this the proper way of doing it / is it fine? Second, if it is, I was trying to pass a Sucess message after they change their email to the /Manage/EmailAddress page, but the ViewBag.StatusMessage is not outputting anything on my HTML page. Why is that?
I did a little more research and here are my findings (tell me if it's correct or not). So I should edit the route settings and so something like:
routes.MapRoute(
name: "Manage",
url: "Account/{controller}/{action}/{id}",
defaults: new { controller = "Manage", action = "Index", id = UrlParameter.Optional}
);
And just make a new controller called Manage and create new Functions inside the controller for different pages like ChangeEmail and ChangePassword?