Reputation: 4733
I have this code:
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);
}
but it throws an exception in int? id = int.Parse(ViewBag.studentId);
it says that my viewbag is null. But it's actually not null because in first time it anyway goes in my studentsDiv action. When I'm debugging id is there for example 1 but on my personalStudentDetails this ViewBag is anyway null
and here's my view:
@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 })
Upvotes: 0
Views: 211
Reputation:
Change your ActionLink() methods to pass the id
as a route parameter to your methods (ditto for 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
Then the methods can be (assuming the ID property is not nullable)
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);
}
Upvotes: 2
Reputation: 3019
Rectify @Ajax.ActionLink() to
@Ajax.ActionLink("Partial details", "personalStudentDetails", new
{
id = Model.ID //Here value
}, new AjaxOptions()
{
HttpMethod = "GET",
UpdateTargetId = "studentsDiv",
InsertionMode = InsertionMode.Replace
}, new { @class = "sactive" })
Upvotes: 0