Posts

Basics of Layout, View and Partial View in MVC.

Image
In this post we will focus on basics of View section of Model View Controller (MVC) architectural pattern. There are three types of Views in MVC. Layout (Master Page) View (Page) Partial View (User Controller) Layout Layout is used to provide consistent experience to the user in terms of Header Footer and Menus throughout the Application / Website. We can also defined commonly used Stylesheets and Scripts in layout so many views in the application can refer it mean we don't need to to define those Sylesheets and Scripts on every View in the application. So here it clears to us Layout eliminates the duplication code in the View. View View is interface or page though which user can interact with the Application or Website. View displays data from Model/Controller to User. To know more about redirection of views visit here   Partial View And the Partial View is View(mark up file) that renders HTML output within another View. So Partial

Difference Between return View(), return Redirect(), return RedirectToAction() And RedirectToRoute() In MVC.

There are different ways of rendering view in MVC. And many of us are confused about them in the beginning stages of our journey as MVC programmers. Here I try my level best to clear up the confusion by explaining the concepts. return View() It tells MVC to generate an HTML template to be displayed and sends it to the browser without making a new request. It does mean that it’s not changing the URL in the browser’s address bar. public  ActionResult Index()   {        return  View();   }   return RedirectToAction() To redirect to a different action which can be in the same or different controller. It tells ASP.NET MVC to respond with a browser to a different action instead of rendering HTML as View() method does. Browser receives this notification to redirect and makes a new request for the new action. return  RedirectToAction(“ActionName”,”ControllerName”); Or this can be written as return  RedirectToAction(“~/ControllerName/ActionName”)

RenderBody() and RenderPage() in MVC.

RenderBody() If you are aware about Web Forms then RenderBody() method is like ContentPlaceHolder in Web Forms. This exists in Layout page and it will render the child View/Page. RenderBody() renders the content of the child which is not wrapped in the section. The RenderBody() method must be present in the Layout view and Layout page will have only one RenderBody() method.And we can't pass any parameter to RenderBody() method. @RenderBody() RenderPage() RenderPage() method also exist in Layout Page to render the other pages exists in the same application. multiple RenderPage() can be there in layout page. @RenderPage("~/Views/Shared/_ Notification.cshtml";) I hope this clears up the basic differences between RenderBody() and RenderPage() in MVC.

Basic Difference Between Convert.ToString() And .ToString() Method In C#

Both these methods are used to convert a string. But yes, there is a difference between them and the main difference is that Convert.Tostring() function handles the NULL while the .ToString() method does not and it throws a NULL reference exception. So, it is a good programming practice to use Convert.ToString() method. But when you want to get an exception if you don't expect your object to be NULL, you must use the .ToString() method. //Below code will throw null reference exception.   object  obj =  null ;   string   str = obj.ToString();      //Below code will not throw an exception and will return String.Empty   object  obj =  null ;   string  str = Convert.ToString(obj); I hope this clears up the basic difference between Convert.ToString() and .ToString() Method in C#.