Asp.net mvc 如何在不同的控制器中返回视图
这是可行的,但如果它位于不同的视图文件夹中,为什么不可行呢?我是不是错过了什么 请注意ASP.net新手 问候Asp.net mvc 如何在不同的控制器中返回视图,asp.net-mvc,Asp.net Mvc,这是可行的,但如果它位于不同的视图文件夹中,为什么不可行呢?我是不是错过了什么 请注意ASP.net新手 问候 Vinay您可以重定向到操作: Class Student { String Name {get; set} //Extended property of the student School Shooling {get; set;} } public StudentControlelr () { public SchoolInfo (int? ID)
Vinay您可以重定向到操作:
Class Student {
String Name {get; set}
//Extended property of the student
School Shooling {get; set;}
}
public StudentControlelr ()
{
public SchoolInfo (int? ID)
{
Student s = StudentRepository.GetStudentByID(id)
Return View ("Schooling/Index", s.Schooling);
}
}
for some reason i have to make this view as a shared view
// Views/Shared/schooling.ascx
Return View ("Schooling", s.Schooling);
这里唯一的问题是您无法传递模型,但有两种方法可以解决此问题:
传递参数并在加载时获取模型
return RedirectToAction("Action", "Controller");
使用TempData传递模型
return RedirectToAction("Action", "Controller", new { id = 1});
您可以重定向到操作:
Class Student {
String Name {get; set}
//Extended property of the student
School Shooling {get; set;}
}
public StudentControlelr ()
{
public SchoolInfo (int? ID)
{
Student s = StudentRepository.GetStudentByID(id)
Return View ("Schooling/Index", s.Schooling);
}
}
for some reason i have to make this view as a shared view
// Views/Shared/schooling.ascx
Return View ("Schooling", s.Schooling);
这里唯一的问题是您无法传递模型,但有两种方法可以解决此问题:
传递参数并在加载时获取模型
return RedirectToAction("Action", "Controller");
使用TempData传递模型
return RedirectToAction("Action", "Controller", new { id = 1});
这将起作用,但更好的解决方案是重定向到不同的操作:
TempData["MyModel"] = s.Schooling;
return RedirectToAction("Action", "Controller");
注意:如果不使用aspx视图,则需要将.aspx更改为视图扩展。这将起作用,但更好的解决方案是重定向到其他操作:
TempData["MyModel"] = s.Schooling;
return RedirectToAction("Action", "Controller");
注意:如果不使用aspx视图,则需要将.aspx更改为视图扩展名