开发者

How to pass one column of data as a list using viewbag to a strongly typed view

Can anyone point me in the right direction as to how to pass one column of data in a list using viewbag to a strongly typed view. I am passing a list of CustomerSites using a strongly typed view. I would also like to display a CustomerName next to each CustomerSite in the list.

I have tried the following approach, but as the controller is returning a list of customers it wont let me access the CustomerName property from the view, is it possible to access this property from the view?

public ViewResult Index()
    {
        var q = from a in db.Customers.ToList()
        select a;
        ViewBag.customer = q;
        return View(db.CustomerSites.ToList());
    }

@model IEnumerable<trsDatabase.Models.CustomerSite>
@foreach (var customer in Model)
{
<tr>
 <td>
        @ViewBag.customer.CustomerName
   </td>
   <td>
        @customer.UnitNo
    </td>
    <td>
        @Truncate(customer.StreetName, 25)
    </td>
    <td>
        @customer.Town
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=customer.Id }) |
        @Html.ActionL开发者_Go百科ink("Details", "Details", new { id=customer.Id }) |
        @Html.ActionLink("Delete", "Delete", new { id=customer.Id })
    </td>
</tr>
}


I would encourage you to pass to the View a ViewModel built out from CustomerSites and CustomerNames. Why don't you get from the DB object which consists from Customer name and site?


It seems to me that ViewBag.customer holds a collection of Customers not a single Customer object. In your Query you should have a where part.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜