MVC 3 Can't pass string as a View's model?

asp.net Mvcasp.net Mvc-3asp.net Mvc-Routing

asp.net Mvc Problem Overview


I have a strange problem with my model passed to the View

Controller

[Authorize]
public ActionResult Sth()
{
    return View("~/Views/Sth/Sth.cshtml", "abc");
}

View

@model string

@{
    ViewBag.Title = "lorem";
    Layout = "~/Views/Shared/Default.cshtml";
}

The error message

The view '~/Views/Sth/Sth.cshtml' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Sth/Sth.cshtml
~/Views/Sth/abc.master  //string model is threated as a possible Layout's name ?
~/Views/Shared/abc.master
~/Views/Sth/abc.cshtml
~/Views/Sth/abc.vbhtml
~/Views/Shared/abc.cshtml
~/Views/Shared/abc.vbhtml

Why can't I pass a simple string as a model ?

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

Yes you can if you are using the right overload:

return View("~/Views/Sth/Sth.cshtml" /* view name*/, 
            null /* master name */,  
            "abc" /* model */);

Solution 2 - asp.net Mvc

If you use named parameters you can skip the need to give the first parameter altogether

return View(model:"abc");

or

return View(viewName:"~/Views/Sth/Sth.cshtml", model:"abc");

will also serve the purpose.

Solution 3 - asp.net Mvc

You meant this View overload:

protected internal ViewResult View(string viewName,	Object model)

MVC is confused by this overload:

protected internal ViewResult View(string viewName,	string masterName)

Use this overload:

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

This way:

return View("~/Views/Sth/Sth.cshtml", null , "abc");

By the way, you could just use this:

return View("Sth", null, "abc");

Overload resolution on MSDN

Solution 4 - asp.net Mvc

It also works if you pass null for the first two parameters:

return View(null, null, "abc");

Solution 5 - asp.net Mvc

It also works if you declare the string as an object:

object str = "abc";
return View(str);

Or:

return View("abc" as object);

Solution 6 - asp.net Mvc

You also write like

return View(model: "msg");

Solution 7 - asp.net Mvc

This seems so pretty obvious but maybe someone needs more clarification in the future:

If in your controller do:

  string id = "abc";
  return View(model: id);

Then in your view, you need:

@model string

In order to get the value, for e.g.:

<div>@Model</div>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTonyView Question on Stackoverflow
Solution 1 - asp.net MvcnemesvView Answer on Stackoverflow
Solution 2 - asp.net Mvcuser474407View Answer on Stackoverflow
Solution 3 - asp.net Mvcgdoron is supporting MonicaView Answer on Stackoverflow
Solution 4 - asp.net MvcAlex DreskoView Answer on Stackoverflow
Solution 5 - asp.net MvcTim MacView Answer on Stackoverflow
Solution 6 - asp.net MvcHiren PatelView Answer on Stackoverflow
Solution 7 - asp.net MvcLeandro BardelliView Answer on Stackoverflow