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.
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.
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);
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.