MVC新手。我试图为继承了内置Identity模型ApplicationUser的两个独立模型(Student和Tutor)复制现有的Identity Register.cshtml。我这样做了,所以我在应用程序中使用了单独的学生和教师模型,但是将它们存储在同一个地方并利用相同的身份用户和角色功能。
我修改了AccountController.cs,使用Register()动作方法作为创建RegisterStudent()和RegisterTutor()动作方法的基础。唯一的区别是我用对相应模型的引用替换了所有ApplicationUser引用。
然后我手动复制了Register视图并将其重命名为RegisterStudent和RegisterTutor,更改了HtmlHelper BeginForm引用。当我导航到那些路径时,我收到了一个"无法找到资源。"错误表明它不喜欢https://localhost:44301/Account/RegisterTutor
的路径。
当我将RegisterTutor.cshtml的内容复制到Register.cshtml文件中时,它就像一个冠军。对我失踪的想法有什么看法?
答案 0 :(得分:0)
你可以做两件事:
RegisterTutor.cshtml
和RegisterStudent.cshtml
内部调用的公共视图(部分视图),这些视图分别从操作RegisterTutor
和RegisterStudent
调用。 / LI>
您可以直接从两者中引用相同的视图(cshtml文件) 行动如下:
public ActionResult RegisterTutor()
{
var _model = 'Model Object'
....
....
return ("~/Account/Register.cshtml", _model)
}
类似地
public ActionResult RegisterStudent()
{
var _model = 'Model Object'
....
....
return ("~/Account/Register.cshtml", _model)
}
您可以将Register.cshtml
与基础模型强绑定,如果属性相同或者您可以创建一个接口,可以用来强烈绑定视图,然后上面的两个模型都应该实现它。