问题描述
我有两个桌子.学生和年级.成绩有一个外键StudentId,所以我知道哪个学生获得了该成绩.
I have two tables. Student and Grade. Grade has a foreign key StudentId, so I know which student got that Grade.
我有一个操作AddStudent,视图有一个窗体,其中将学生数据数据插入数据库.AddStudent操作将返回PartialView.在该PartialView中,我有新的成绩表.
I have an action AddStudent and view has a form, where I insert student data data into database. AddStudent action returns PartialView. In that PartialView I have new form for Grades.
我需要以某种方式获取StudentId(在AddStudent动作中插入数据后应该以某种方式获取).如何从AddStudent操作将StudentId传递到成绩"局部视图?因为我需要在StudentId中插入成绩",所以我知道要针对哪个学生成绩.
I need to somehow get StudentId(which I should somehow get after I insert data in AddStudent action). How can you pass StudentId from AddStudent action to Grades partial view? Because I need to insert "Grades" with StudentId so I know to which student Grades are intended.
动作:
[HttpPost]
[ValidateAntiForgeryToken]
public virtual ActionResult CreateStudent(Student student)
{
if (ModelState.IsValid)
{
db.Students.Add(student);
db.SaveChanges();
return PartialView(MVC.Student.Views._AddGrades); // do I have to pass student.Id here to partialView somehow?
}
return View(student);
}
成绩评分:
[HttpPost]
[ValidateAntiForgeryToken]
public virtual ActionResult CreateGrade(Grade grade, int studentId)
{
if (ModelState.IsValid)
{
grade.studentId = studentId; // how to get this from previous action to my partial view?
db.Grades.Add(grade);
db.SaveChanges();
return PartialView(MVC.Grades.Views._UpdateTableOfGrades);
}
return View(grade);
}
我真的很感谢一些例子.
I would really be thankful for some example.
谢谢!
推荐答案
我认为您应该在控制器上添加Get CreateGrade操作并返回RedirectToAction,如下所示:
I think you should add a Get CreateGrade action on your controller and return a RedirectToAction like this :
return RedirectToAction(MVC.Student.CreateGrade)
您还需要为创建的模型创建一个模型,其中将包含StudentId属性.
You also need to create a Model for the creation that will contain the StudentId property.
如果您使用T4MVC,则可以执行以下操作:
If you use T4MVC you can do this :
return RedirectToAction(MVC.Student.CreateGrade).AddRouteValue("studentid", student.Id);
这篇关于Asp.Net MVC-将最后创建的ID从操作传递到局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!