Asp.net-Mvc

MVC 3 不能將字元串作為視圖模型傳遞?

  • March 21, 2012

我的模型傳遞給視圖有一個奇怪的問題

控制器

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

看法

@model string

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

錯誤資訊

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

為什麼我不能將簡單的字元串作為模型傳遞?

是的,如果您使用正確的重載,您可以:

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

引用自:https://stackoverflow.com/questions/9802546