我的Razor页面上的@model不起作用 [英] @model on my Razor page doesn't work
问题描述
我试图运行我的剃须刀页面,但我不断收到此错误:
I am trying to get my razor page to run but I keep getting this error:
ASP._Page_Views_profile_add_cshtml.Execute()':找不到适合的方法来覆盖
ASP._Page_Views_profile_add_cshtml.Execute()': no suitable method found to override
在进行一些研究时,我发现我需要在web.config中添加一些已经完成的操作,而且还需要在顶部添加"@model"并提供一个模型.到目前为止,我有这个:
and in doing some research I have found out that I needed to add some things to the web.config which I have done but also that I need to add the "@model" to the top and provide a model. So far I have this:
@model ProfileViewModel
@{
ViewBag.Title = "Add";
Layout = "~/Views/Shared/_Master.cshtml";
}
@model
关键字不起作用,它没有像@inherits
关键字那样突出显示为黄色,我相信这是我的问题,但不知道如何解决.有人可以帮忙吗?
the @model
keyword does not work, its not highlighted yellow like the @inherits
keyword would be, I believe that is my problem but no clue on how to fix it. Can someone please help?
推荐答案
在Visual Studio设计器中,@model
关键字不会以黄色突出显示.它需要指向一个有效的类,这样,如果将光标放在该类上并按 F12 ,则应导航到相应的类定义.如果这没有发生,则可能需要指定完整类型名称,包括名称空间.
The @model
keyword won't be highlighted in yellow in the Visual Studio designer. It needs to point to a valid class so that if you put the cursor over it and press F12 you should navigate to the corresponding class definition. If this doesn't happen you might need to specify the full type name including the namespace.
呈现此特定视图的控制器动作也需要传递模型实例:
Also the controller action that is rendering this particular view needs to pass an instance of the model:
public ActionResult Foo()
{
ProfileViewModel model = ...
return View(model);
}
这篇关于我的Razor页面上的@model不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!