0

I'm still quite new to ASP.Net MVC, so maybe this is a stupid question.

I'm using MVC 3 with razor syntax.

My question is, how do I show some HTML after a successfull post ?

I've got a password recovery page, and upon posting correct information I'd like it to show the view again, but add a "A new password has been sent to your e-mail address" to it.

In WebForms I'd just have a invisible panel and show it, or a literal and stuff some text into it on postback. What do I do in MVC ?

I suppose I could add a different view for it, but really that seems a bit like overkill - or am I misunderstand the MVC paradigm here ?

EDIT:

I tried using the strongly typed model for this, but it doesn't work :-(

My view (simplified a bit):

@model BudgetPal.Model.MVC.Account.EditModel
@if (Model.ShowConfirmation)
{
    <div class="confirmation">Your profile has been saved.</div>
}

The Model:

public class RecoverPasswordModel
{
    [Required]
    [Display(Name = "E-mail")]
    [StringLength(250)]
    [RegularExpression(@"([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})", ErrorMessage = "Not a valid e-mail address")]
    public string Email { get; set; }

    public bool ShowConfirmation { get; set; }
}

And my controller:

public ActionResult Edit()
        {
            EditModel model = new EditModel(UserSession.User);
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(EditModel model)
        {
            if (!ModelState.IsValid)
                return View(model);

            // Save changes here

            model.ShowConfirmation = true;
            return View(model);
        }

1 Answer 1

7

You don't need to add a separate view. The quick solution would be to set a property on the dynamic ViewBag object and check for its existence inside your view.

In your controller, set the dynamic ViewBag property...

ViewBag.Message = "A new password has been sent to your email address.";

In your view, something like...

@if(ViewBag.Message != null) {
    <div class="message">@ViewBag.Message</div>
}

You can find some more information here.

If you are using a strongly-typed view, you can simply add a property to your view model object that will store the message, and check it in the same manner you check the dynamic ViewBag property to determine whether or not to display the message.

Sign up to request clarification or add additional context in comments.

4 Comments

Actually sorry to bother you again. I tried the approach with a strong typed model, and for some reason it doesn't show up. I've edited the question to clarify.
Can you post the code of your view model and the @model declaration at the top of your view? Did you try the ViewBag method successfully?
I just tried using the ViewBag - same result :-( I've added a complete code example to my original post.
Ok now I figured it out. Upon doing the post I'm not given the new source in chrome, so therefore I thought it didn't render it. It did however, it's just that the class is display hidden (old style I hadn't cleaned up) DOH. So your example works just fine, it's my bad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.