Please enable Javascript for better experience...
How can I pass string value as a model in View in MVC
By Rahul Kumar Jha | Dec 8, 2014 | In Articles | Total Views [ 7499 ]
Taged In
(4 Like)
Rate

We all know that we can pass a model to view to display content in MVC but there are certain scenarios where passing string value as a model seems a problem. In this article I will try to explain how can we pass string as model in a view.

Introduction

MVC stands for Model-View-Controller; and we are very much familier with this. We all know, how these three works together in building a powerful web application. Model usually used as a container which can pass data from Data Layer through Business Layer to a View with the help of Controllers. Passing model to a view is generally very easy and we do this in almost all the methods created in a controller. Then why I am writing this article. The reason is, I faced a problem in my early days of learning while passing a string as a model. Let's see what I am talking about.

Actual Problem

In general practice, we render a view with or without passing any model and inside it we render multiple partial views with models. You will not face any problem while passing model to a partial view because of it's overloads available. You can pass any type of model and it will accept with no problem.

A partial view has four overloads:

protected internal PartialViewResult PartialView();
protected internal PartialViewResult PartialView(object model);
protected internal PartialViewResult PartialView(string viewName);   //It will create confusion between view name and model of string type
protected internal virtual PartialViewResult PartialView(string viewName, object model);

Even you will not face any trouble when passing a model to a View because view can handle this request by its overloads available.

protected internal ViewResult View(object model);

or

protected internal ViewResult View(string viewName, object model);
But think of a scenario where you want to pass a string value as a model to a view/partial view, it will start throwing an exception.

And the reason behind this is that it will conflict with its overload. Actually it is assuming string value as view name or master name and not a model.

protected internal PartialViewResult View(string viewName);
protected internal PartialViewResult PartialView(string viewName);

or

protected internal ViewResult View(string viewName, string masterName);

What is the solution?

To overcome this problem, let's see what alternates we can use. View have nearly eight overload methods for different purposes.

View's overloads:

protected internal ViewResult View();
protected internal ViewResult View(IView view);
protected internal ViewResult View(object model);
protected internal ViewResult View(string viewName);
protected internal virtual ViewResult View(IView view, object model);
protected internal ViewResult View(string viewName, object model);
protected internal ViewResult View(string viewName, string masterName);
protected internal virtual ViewResult View(string viewName, string masterName, object model);

To pass string value as a model you can use below overload

protected internal virtual ViewResult View(string viewName, string masterName, object model);

You can also directly use model: keyword with your model to achieve this. Let see these with an example.

 /// <summary>
        /// Using View(object model) overload
        /// </summary>
        /// <returns></returns>
        public ViewResult Info()
        {
            return View(model:"testing sending string as model");
        }

        /// <summary>
        /// Using View(string viewName, object model) overload
        /// </summary>
        /// <returns></returns>
        public ViewResult CallInfo()
        {
            return View("Info", model: "testing sending string as model");
        }

        /// <summary>
        /// Using View(string viewName, string masterName, object model) overload
        /// </summary>
        /// <returns></returns>
        public ViewResult CallInfo()
        {
            return View("Info",null,"testing sending string as model");
        }

You can use any overload method of View as per your convenience and will get result.  Hope this can help you. Use it in your code and try.

Share this

About the Author

Rahul Kumar Jha
Rahul Kumar Jha
Founder, Developer dotnet-concept.com

Public profile: user/profile/99900001


Has working experience in different phases of Software Development Life Cycle (SDLC) in CMS, Gaming, Health Care and Financial Services domain using Agile pattern. Working experience in Design patterns, ASP.NET, MVC, ANGULAR, ANGULAR JS, Windows application, WCF, ADO.NET, SQL Server and Test Driven Development (TDD) environment with JQuery, JavaScript, N-Unit, Entity Frameworks, LINQ, Code Refactoring and Business Objects Models.

User's Comments


 
Please SignUp/Login to comment...

Or comment as anonymous...
* Name
* Email ID
Comment
 
 
 
 
 
 
Sponsors