[SOLVED] Can’t get the data in viewbag – Stack Overflow

Issue

This Content is from Stack Overflow. Question asked by Mark John Capistrano

@model SPES_PPS.Models.Benificiary

My partial view

@foreach (var item in ViewBag.bendetails)
{
    <tr>
        <td>Beni ID</td>
        <td>@item.benID</td>
    </tr>
    <tr>
        <td></td>
        <td></td>
    </tr>
}

my Jquery in view

controller

// GET: Benificiary/Details/
public ActionResult Details(int id)
{
{
//var benificiary = db.Benificiaries.Where(x => x.id == id).FirstOrDefault();
//ViewBag.showdetails = benificiary;
//return PartialView(“Partial_Details”);

Benificiary benificiary = db.Benificiaries.Find(id);
if (benificiary == null)
{
return HttpNotFound();
}
ViewBag.bendetails = benificiary;
return PartialView(“Partial_Details”, benificiary);

}
}



Solution

In your Action, You return a PartialView with a model, Why you get data from ViewBag?

You use db.xxx.Find(id) to select a Benificiary, Why you use foreach()?

You can try to change your code like in Partial view.

@model SPES_PPS.Models.Benificiary

<h3>My partial view</h3>

<table>
        <tr>
            <td>Beni ID</td>
            <td>@Model.benID</td>
        </tr>
        <tr>
            <td></td>
            <td></td>
        </tr>    
    
    </table>


This Question was asked in StackOverflow by Mark John Capistrano and Answered by Xinran Shen It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?