如何将 id 发送给其他控制器进行创建?
How to send id to other controller for create?
我正在尝试创建 table,它显示来自其他 table 的项目列表,外键为当前 table,并且能够创建 ID 为当前 [=] 的新行24=].
我已经尝试创建 RouteMap:
routes.MapRoute(
name: "GroupsStudents",
url: "{controller1}/{id}/{controller2}/{action}",
defaults: new { controller1 = "Groups", id =
UrlParameter.Optional, controller2 = "Students", action =
"Create" });
在学生中:
public ActionResult Create(int id)
{
ViewBag.GruopID = new SelectList(db.Groups, "GroupID", "Name",
db.Groups.First(p => p.GroupID == id));
return View();
}
但我收到此错误:匹配的路由不包含 'controller' 路由值,这是必需的。
有我的 tables:
public class Group
{
public int GroupID { get; set; }
[Required]
public string Name { get; set; }
[ForeignKey("GroupID")]
ICollection<Student> Students { get; set; }
}
public class Student
{
public int StudentD { get; set; }
public int GroupID { get; set; }
public Group Group { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Surname { get; set; }
}
也许你可以这样定义你的路线:
routes.MapRoute(
name: "GroupsStudents",
url: "Groups/{groupId}/{controller}/{action}",
defaults: new {controller = "Students", action = "Create" });
public ActionResult Create(int groupId)
{
ViewBag.GruopID = new SelectList(db.Groups, "GroupID", "Name",
db.Groups.First(p => p.GroupID == groupId));
return View();
}
此外,我认为 groupId 参数不应该是可选的。
我正在尝试创建 table,它显示来自其他 table 的项目列表,外键为当前 table,并且能够创建 ID 为当前 [=] 的新行24=].
我已经尝试创建 RouteMap:
routes.MapRoute(
name: "GroupsStudents",
url: "{controller1}/{id}/{controller2}/{action}",
defaults: new { controller1 = "Groups", id =
UrlParameter.Optional, controller2 = "Students", action =
"Create" });
在学生中:
public ActionResult Create(int id)
{
ViewBag.GruopID = new SelectList(db.Groups, "GroupID", "Name",
db.Groups.First(p => p.GroupID == id));
return View();
}
但我收到此错误:匹配的路由不包含 'controller' 路由值,这是必需的。
有我的 tables:
public class Group
{
public int GroupID { get; set; }
[Required]
public string Name { get; set; }
[ForeignKey("GroupID")]
ICollection<Student> Students { get; set; }
}
public class Student
{
public int StudentD { get; set; }
public int GroupID { get; set; }
public Group Group { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Surname { get; set; }
}
也许你可以这样定义你的路线:
routes.MapRoute(
name: "GroupsStudents",
url: "Groups/{groupId}/{controller}/{action}",
defaults: new {controller = "Students", action = "Create" });
public ActionResult Create(int groupId)
{
ViewBag.GruopID = new SelectList(db.Groups, "GroupID", "Name",
db.Groups.First(p => p.GroupID == groupId));
return View();
}
此外,我认为 groupId 参数不应该是可选的。