Monday 15 April 2013

c# - The popular model item passed into the dictis of type System.Collections.Generic.List`1[X] but this dictionary requires a model item of type X -



c# - The popular model item passed into the dictis of type System.Collections.Generic.List`1[X] but this dictionary requires a model item of type X -

so yeah have investigated , read lot of responses here have mismatch info types.. cannot figure out why happening.

public class restaurantbranchmodel { public int id { get; set; } public string name { get; set; } public string telephone { get; set; } public int master_restaurant_id { get; set; } //public restaurantmodel master_restaurant { get; set; } public int address_id { get; set; } //public addressmodel address { get; set; } }

controller

restaurantbranchrepository restaurantbranchrepository = new restaurantbranchrepository(); ienumerable<restaurantbranchmodel> branches; // get: restaurantbranch public actionresult index() { branches = restaurantbranchrepository.getbranches(); homecoming view(branches); //i've tried adding .tolist() }

view

@model ienumerable<ordenarbackend.models.restaurantbranchmodel> @{ viewbag.title = "index"; layout = "~/views/shared/_xenonlayoutpage.cshtml";} <h2>index</h2> <p> @html.actionlink("create new", "create") </p> <table class="table"> <tr> <th> @html.displaynamefor(model => model.name) </th> <th> @html.displaynamefor(model => model.telephone) </th> <th> @html.displaynamefor(model => model.master_restaurant_id) </th> <th> @html.displaynamefor(model => model.address_id) </th> <th></th> </tr> @foreach (var item in model) { <tr> <td> @html.displayfor(modelitem => item.name) </td> <td> @html.displayfor(modelitem => item.telephone) </td> <td> @html.displayfor(modelitem => item.master_restaurant_id) </td> <td> @html.displayfor(modelitem => item.address_id) </td> <td> @html.actionlink("edit", "edit", new { id=item.id }) | @html.actionlink("details", "details", new { id=item.id }) | @html.actionlink("delete", "delete", new { id=item.id }) </td> </tr> } </table> @section bottomscripts{}

//end of code

so.. have model. pass collection of items in view, view under list template mvc, , says passing generic list needs single object ? gives??..

this error

the model item passed dictionary of type 'system.collections.generic.list`1[ordenarbackend.models.restaurantbranchmodel]', dictionary requires model item of type 'ordenarbackend.models.usermodel'.

change

@html.displaynamefor(model => model.name)

to

@html.displaynamefor(model => model.first().name)

basically needs single object , passing in list.

c# model-view-controller

No comments:

Post a Comment