How to create a single thanks page for entire controller in MVC

Sometimes we need a thanks page say we have user registration, change password, activate account functionality in our application then we need a thanks page after registering with our site, to say thanks for registering with us or showing confirmation that your password is successfully changed or your account is successfully activated. So do you think we need three different view for these confirmation page, no need to create three different page, simply we can create a single thanks page and it will server not only for these three confirmation page but for entire application. So how to change the title, and message for different pages, let’s create a model to pass data and render from this model

public class ThanksModel
{
    public string PageTitle { get; set; }
    public string Message { get; set; }
    public bool IsSuccess { get; set; }        
}

Now let’s create a strongly typed view and name it Thanks

@model AdvanceSharp.Webapp.Models.ThanksModel
@{
   ViewBag.Title = @Model.PageTitle;
   Layout = "~/Views/Shared/layout.cshtml";
}

<h2>@Model.PageTitle</h2>
<div style="min-height: 300px;">
  @if(Model.IsSuccess){
    <p>
      @MvcHtmlString.Create(@Model.Message)
    </p>
  }
  else{
    <p style="color:Red">
         @MvcHtmlString.Create(@Model.Message)
   </p>
  }

We use @MvcHtmlString.Create to render the message so we can pass message in HTML format as well.

Let's see how to use this view, from different controller methods

[HttpPost]
public ActionResult Register(RegistrationModel register)
{
   //Code to save detail in detabase
   return View("Thanks", 
      new ThanksModel { PageTitle = "Registeration",
       Message = "<div>Thanks for registering with us</div>", 
       IsSuccess = true });
}

As you can see we are returning a different view, so the URL will be the same and content will be from thanks view, which we are rendering dynamically by using ThanksModel.

No need to create controller method, as you see we never created it.

As we used for Registration similarly we can use it from any controller method.

Hamden Process manager with a reputed organization, Fond of learning new features and technology related to C#, ASP.Net, SQL Server, MVC etc.I like to help others, if I can
  • mvc
  • dynamic-view
By Hamden On 30 Jun, 13  Viewed: 3,771

Other blogs you may like

mvc search page example with code

MVC Searh page with pagination: It’s very easy to create a search page in asp.net but when I try to create the same in MVC I faced many problems, how to create model, how to keep searched values in search controls, pagination, I found myself nowhere, so start searching for some good examples but... By Ali Adravi   On 25 Aug 2013  Viewed: 40,241

MVC insert update delete and select records

CRUD (Create, Retrieve, Update and Delete) in MVC. When we start to learn new language, first we try to run an application with “Hello World” and then CRUD functionality. So in this article we will see how to select records from database (with WebGrid, pagination and sort functionality), update a... By Ali Adravi   On 17 Aug 2013  Viewed: 106,061

MVC jquery autocomplete with value and text field

In MVC, autocomplete with jquery is the best way to pull data from database and render according to our requirements, so in this article we will use jquery to show the auto complete text box without any ajax controls. We need a method to get the data from database, a controller method to handle the... By Ali Adravi   On 29 Jun 2013  Viewed: 7,022

Upload files with model data in MVC

Upload multiple files with model data in MVC is really very easy, when I started to test by uploading some files, I though it would be more complicated but it is really not. In my previous post [ASP.Net MVC file upload][1], I promised to post soon about how to upload multiple files. When I was... By Ali Adravi   On 04 Jun 2013  Viewed: 25,571

ASP.Net MVC file upload

How to upload file or image in asp.net mvc is not difficult but is totally different compare to asp.net FileUpload control. If we are asp.net developer and we are new to mvc then we immediately noticed we don’t have server side controls so the FileUpload control as well and our problem start here.... By Ali Adravi   On 25 May 2013  Viewed: 8,088