我有这段代码:
public PartialViewResult studentsDiv(int? id)
{
if (id == null)
{
id = ViewBag.studentId;
}
else
{
ViewBag.studentId = id;
}
ACD_UNI_STUDENTS students = db.ACD_UNI_STUDENTS.Find(id);
return PartialView(students);
}
public PartialViewResult personalStudentDetails()
{
int? id = int.Parse(ViewBag.studentId);
ACD_UNI_STUDENTS students = db.ACD_UNI_STUDENTS.Find(id);
return PartialView(students);
}
但它在int? id = int.Parse(ViewBag.studentId);
中引发异常,它表示我的viewbag为null。但它实际上不是空的,因为在第一次它无论如何进入我的 studentsDiv 行动。当我调试id时,例如1,但在我的 personalStudentDetails 上,这个ViewBag无论如何都是null
这是我的观点:
@Ajax.ActionLink("Full details", "studentsDiv", new
{
}, new AjaxOptions()
{
HttpMethod = "GET",
UpdateTargetId = "studentsDiv",
InsertionMode = InsertionMode.Replace
}, new { @class = "sactive", id = Model.ID })
|
@Ajax.ActionLink("Partial details", "personalStudentDetails", new
{
}, new AjaxOptions()
{
HttpMethod = "GET",
UpdateTargetId = "studentsDiv",
InsertionMode = InsertionMode.Replace
}, new { @class = "sactive", id = Model.ID })
答案 0 :(得分:2)
更改ActionLink()方法,将id
作为路径参数传递给您的方法(同上personalStudentDetails
)
@Ajax.ActionLink("Full details", "studentsDiv",
new { id = Model.ID },
new AjaxOptions()
{
HttpMethod = "GET",
UpdateTargetId = "studentsDiv",
InsertionMode = InsertionMode.Replace
},
new { @class = "sactive" }) // assume you don't really need an id attribute
然后方法可以是(假设ID属性不可为空)
public PartialViewResult studentsDiv(int id)
{
ACD_UNI_STUDENTS students = db.ACD_UNI_STUDENTS.Find(id);
return PartialView(students);
}
public PartialViewResult personalStudentDetails(int id)
{
ACD_UNI_STUDENTS students = db.ACD_UNI_STUDENTS.Find(id);
return PartialView(students);
}
答案 1 :(得分:0)
将@ Ajax.ActionLink()改编为
@Ajax.ActionLink("Partial details", "personalStudentDetails", new
{
id = Model.ID //Here value
}, new AjaxOptions()
{
HttpMethod = "GET",
UpdateTargetId = "studentsDiv",
InsertionMode = InsertionMode.Replace
}, new { @class = "sactive" })