@model on my Razor page doesn't work

末鹿安然 提交于 2019-12-25 14:00:20

问题


I am trying to get my razor page to run but I keep getting this error:

ASP._Page_Views_profile_add_cshtml.Execute()': no suitable method found to override

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";
}

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?


回答1:


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


来源:https://stackoverflow.com/questions/4961758/model-on-my-razor-page-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!