c# - Details View Controller method using viewModel -
i'm still finding feet asp.net mvc.
i want generate details view viewmodel i'm not getting controller right saying i'm passing incorrect type of model view.
the controller is:
public actionresult clubdetails(int clubid) { //populate view var clubs = s in db.clubs orderby s.name select s; var viewmodel = clubs.select(t => new clubdetailsviewmodel { name = t.name, shortname = t.shortname, founded = t.founded, contactname = t.firstname + " " + t.lastname, address1 = t.address1, address2 = t.address2, city = t.city, county = t.county, postcode = t.postcode, telephone = t.telephone, email = t.email, bio = t.bio, website = t.website, }); return view(viewmodel); } the viewmodel is:
public class clubdetailsviewmodel { [display(name = "club name")] public string name { get; set; } [display(name = "abbreviation")] public string shortname { get; set; } [display(name = "founded")] [datatype(datatype.date)] [displayformat(dataformatstring = "{0:dd/mm/yyyy}", applyformatineditmode = true)] public datetime? founded { get; set; } [display(name = "contact name")] public string contactname { get; set; } [display(name = "address 1")] public string address1 { get; set; } [display(name = "address 2")] public string address2 { get; set; } [display(name = "city")] public string city { get; set; } [display(name = "county")] public string county { get; set; } [display(name = "postcode")] public string postcode { get; set; } [display(name = "telephone")] public string telephone { get; set; } [emailaddress] [display(name = "email")] public string email { get; set; } [display(name = "website")] [url] public string website { get; set; } [display(name = "bio")] public string bio { get; set; } } the view is:
@model grcwebapp.viewmodels.clubdetailsviewmodel @{ viewbag.title = "clubdetails"; } <h2>clubdetails</h2> <div> <h4>clubdetailsviewmodel</h4> <hr /> <dl class="dl-horizontal"> <dt> @html.displaynamefor(model => model.name) </dt> <dd> @html.displayfor(model => model.name) </dd> <dt> @html.displaynamefor(model => model.shortname) </dt> <dd> @html.displayfor(model => model.shortname) </dd> <dt> @html.displaynamefor(model => model.founded) </dt> <dd> @html.displayfor(model => model.founded) </dd> <dt> @html.displaynamefor(model => model.contactname) </dt> <dd> @html.displayfor(model => model.contactname) </dd> <dt> @html.displaynamefor(model => model.address1) </dt> <dd> @html.displayfor(model => model.address1) </dd> <dt> @html.displaynamefor(model => model.address2) </dt> <dd> @html.displayfor(model => model.address2) </dd> <dt> @html.displaynamefor(model => model.city) </dt> <dd> @html.displayfor(model => model.city) </dd> <dt> @html.displaynamefor(model => model.county) </dt> <dd> @html.displayfor(model => model.county) </dd> <dt> @html.displaynamefor(model => model.postcode) </dt> <dd> @html.displayfor(model => model.postcode) </dd> <dt> @html.displaynamefor(model => model.telephone) </dt> <dd> @html.displayfor(model => model.telephone) </dd> <dt> @html.displaynamefor(model => model.email) </dt> <dd> @html.displayfor(model => model.email) </dd> <dt> @html.displaynamefor(model => model.website) </dt> <dd> @html.displayfor(model => model.website) </dd> <dt> @html.displaynamefor(model => model.bio) </dt> <dd> @html.displayfor(model => model.bio) </dd> </dl> </div> <p> @html.actionlink("edit", "edit", new { /* id = model.primarykey */ }) | @html.actionlink("back list", "index") </p>
you're passing ienumerable<clubdetailsviewmodel>, not clubdetailsviewmodel. want filter clubs , pick one, instead of doing of them. this:
var viewmodel = db.clubs .where(t => t.clubid == clubid) .select(t => new clubdetailsviewmodel { .. }) .firstordefault(); return view(viewmodel);
Comments
Post a Comment