Note that there are some explanatory texts on larger screens.

plurals
  1. POReturning a list to strongly typed view using viewbag
    primarykey
    data
    text
    <p>I am trying to return a list of customers to a strongly typed view using the viewbag and am having some difficulties as im new to MVC and Razor, can anyone kindly offer any advice on the following problem please?</p> <p>I have the following Code in my controller, </p> <pre><code> public ViewResult Index() { var q = from a in db.Customers select a; ViewBag.customer = q; return View(db.CustomerSites.ToList()); } </code></pre> <p>and this code in my view</p> <pre><code>@foreach (var customer in Model) { &lt;tr&gt; &lt;td&gt; @ViewBag.customer.CustomerName &lt;td&gt; &lt;td&gt; @customer.UnitNo &lt;/td&gt; &lt;td&gt; @Truncate(customer.StreetName, 25) &lt;/td&gt; &lt;td&gt; @Html.ActionLink("Edit", "Edit", new { id=customer.Id }) | @Html.ActionLink("Details", "Details", new { id=customer.Id }) | @Html.ActionLink("Delete", "Delete", new { id=customer.Id }) &lt;/td&gt; &lt;/tr&gt; } </code></pre> <p>So i am retrieving a list of customers and customer sites, the sites are typed to the view, </p> <pre><code>@model IEnumerable&lt;trsDatabase.Models.CustomerSite&gt; </code></pre> <p>I am trying extract CustomerName from the list of customers using </p> <pre><code>@ViewBag.customer.CustomerName </code></pre> <p>But this is generating an error saying the "Customer" type doesnt contain a defintion for customer name but it does as shown below so im not sure why the error is occuring.</p> <pre><code>namespace trsDatabase.Models { public class Customer { [ScaffoldColumn(false)] public int Id { get; set; } [StringLength(50), Required(ErrorMessage = "Customer name required, please enter a customer name")] public string CustomerName { get; set; } [DisplayName("Contact Name")] [StringLength(50), Required(ErrorMessage = "Primary contact name required, please enter a contact name")] public string PrimaryContactName { get; set; } [DisplayName("Secondary Contact")] public string SecondaryContactName { get; set; } [DisplayName("Email")] [StringLength(50), Required(ErrorMessage = "Primary email address is required please enter an email address")] public string PrimaryEmailAddress { get; set; } [DisplayName("Secondary Email")] public string SecondaryEmailAddress { get; set; } [ScaffoldColumn(false)] public DateTime RegisteredDate { get; set; } [DisplayName("Contact No")] public string PrimaryContactNo { get; set; } [DisplayName("Secondary Contact No")] public string SecondaryContactNo { get; set; } [DisplayName("Waste Carrier Ref")] public string WasteCarrierRef { get; set; } [DisplayName("Unit No")] public string UnitNo { get; set; } [DisplayName("Street Name")] [StringLength(50), Required(ErrorMessage = "Street name required, please enter a street name ")] public string StreetName { get; set; } [StringLength(50), Required(ErrorMessage = "Town required, please enter a town")] public string Town { get; set; } [StringLength(50), Required(ErrorMessage = "County is required, please enter a county")] public string County { get; set; } [StringLength(10), Required(ErrorMessage = "Postcode is required, please enter a postcode")] public string Postcode { get; set; } public List&lt;CustomerSite&gt; CustomerSites { get; set; } } } </code></pre> <h3>Update:</h3> <p>I still cant get this to do what I need, I have created a master view model that includes </p> <pre><code>public IEnumerable &lt;Customer&gt; SomeCustomer { get; set; } public IEnumerable &lt;CustomerSite&gt; CustomerSites { get; set; } </code></pre> <p>Then in my view, </p> <pre><code>@model IEnumerable&lt;trsDatabase.Models.masterViewModel&gt; </code></pre> <p>If i try to access the model by typing using a for each </p> <pre><code>@foreach (var customer in Model) </code></pre> <p>When I type @customer. It will only let me access the two lists not the individual properties in the list.</p> <p>I thought I would have been able to do this with e.g. @customer.CustomerSites.UnitNo</p> <p>But the furthest I can go is @customer.CustomerSites</p> <p>Do you have any ideas on what I'm doing wrong here?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload