0

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?

Moe Bataineh
  • 1,032
  • 1
  • 12
  • 29
  • Your Viewbag Setter is commented out. Do you know this? – Botonomous Jul 05 '13 at 21:36
  • ViewBag.ReturnUrl = Url.Action("Manage/" + type); has two // before the code. That is a comment and is skipped by the interpreter. Remove them and Viewbag.ReturnURL will be set. – Botonomous Jul 05 '13 at 21:41
  • Yeah, sorry If my explanation was confusing. I needed to know why ViewBag.StatusMessage = MessagesEnum.ChangePasswordSuccess; was not setting and displaying on my html page when i put @ViewBag.StatusMessage – Moe Bataineh Jul 05 '13 at 21:46

1 Answers1

0

ViewBag and ViewData are only valid for the current request, they wont survive if you do a redirect RedirectToAction. Use TempData instead. Check the following: ViewBag, ViewData and TempData

If you want your URLs to be /Manage/Password and /Manage/EmailAddress, you dont need to add a new MapRoute, the default one will work. Just use ManageController for you controller class name and "Password" and "EmailAddress" for your function names:

class ManageController
{
    public ActionResult Password(...)
    { 
Community
  • 1
  • 1
fcuesta
  • 4,429
  • 1
  • 18
  • 13
  • Alright, It's working. I have one more question, I added a EDIT to my post starting at the point "I did a little more research". Is that method also a good method to do things? – Moe Bataineh Jul 05 '13 at 23:03
  • Do you want your URLs to be Account/Manage/Password or just /Manage/Password? – fcuesta Jul 05 '13 at 23:35
  • Well currently how it is setup, I have to use a SELECT case for the different types of links like Account/Manage/LINKS_HERE. Can you basically setup a Map Route like I showed above so instead of having the items in the AccountController, I can create a ManageController but the link will still be Account/Manage/LINKS_HERE and create functions for different LINKS_HERE? – Moe Bataineh Jul 06 '13 at 00:13