2014-07-22 42 views
0

由于某种原因,视图引擎正在根据字符串值搜索视图,我将其作为视图模型传递。我使用ASP.NET MVC 4ASP.NET MVC View looking by Model value

这是我的操作方法的简化版本。这显示在没有发现标签错误情况,而是或返回默认查看我返回不同的错误页面视图“TagNotFound”:

public ActionResult Tagged() 
    { 
     string tag = "SomeValue"; 

     return View("TagNotFound", tag); 
    } 

TagNotFound.cshtml存在,但搜索是错误的观点。这是我的错误:

The view 'TagNotFound' or its master was not found or no view engine supports the searched locations. The following locations were searched: 
~/Views/Tag/SomeValue.cshtml 
~/Views/Tag/SomeValue.vbhtml 
~/Views/Shared/SomeValue.cshtml 
~/Views/Shared/SomeValue.vbhtml 

而不是使用一个类来调用正确的过载的建议我投的字符串作为一个对象。

return View("TagNotFound", (object)tag); 
+0

你定制任何ASp.NET MVC框架组件?你是否也有任何自定义路线? –

+0

没有自定义。大卫下面解释了为什么会这样。 – David

回答

4

这是因为the overload for the View() method需要字符串是视图的名称。

因此,字符串本身不能成为视图模型。相反,你可能会考虑把任何字符串中的ViewBag

ViewBag.tag = "SomeValue"; 
return View(); 

或者只有一个创造价值的一个视图模型:

public class TagViewModel 
{ 
    public string Tag { get; set; } 
} 

// elsewhere... 

return View(new TagViewModel { Tag = "SomeValue" }); 
相关问题