.net - Model binding to Lists -



.net - Model binding to Lists -

how create default modelbinding handle lists?

let’s have shoppingcart class, has shoppingcartitem list:

public class shoppingcart public property couponcode string public property items new list(of shoppingcartitem) end class public class shoppingcartitem public property title string public property count integer end class

my view looks (as per advice this, , this, blogpost):

@modeltype vavtag.luckydraw.shoppingcart @code layout = nil end code <!doctype html> <html> <head runat="server"> <title>index</title> </head> <body> @using html.beginform() @<fieldset> <legend>shoppingcart</legend> @html.textboxfor(function(m) m.couponcode) <hr /> @html.editorfor(function(m) m.items) <p> <input type="submit" value="save" /> </p> </fieldset> end using </body> </html>

the items rendered using editortemplate (thanks darin):

@modeltype shoppingcartitem @html.editorfor(function(m) m.title ) @html.editorfor(function(m) m.count ) <br />

in controller, i’m inserting random data, , view renders shoppingcart nicely, 3 items.

public class shoppingcartcontroller inherits system.web.mvc.controller function index() actionresult dim model new shoppingcart model.items.add(new shoppingcartitem {.count = 1, .title = "item a"}) model.items.add(new shoppingcartitem {.count = 17, .title = "item b"}) model.items.add(new shoppingcartitem {.count = 100, .title = "item c"}) homecoming view(model) end function <httppost()> function index(model shoppingcart) actionresult homecoming view(model) ' model empty! end function end class

however, when submit page, no values picked up, not couponcode field. model object empty. gives?

ultimately, goal add/remove items clientside via javascript , have modelbinder pick changes automatically when page submitted.

update: missing property keyword model properties declarations. it's late, , got sleep. :)

how create default modelbinding handle lists?

the default model binder already handles lists , dictionaries fine.

just utilize editor templates. example:

view model:

public class shoppingcartitem { public string title { get; set; } public int count { get; set; } } public class shoppingcart { public string couponcode { get; set; } public list<shoppingcartitem> items { get; set; } }

controller:

public class homecontroller : controller { public actionresult index() { var model = new shoppingcart { items = new[] { new shoppingcartitem { count = 1, title = "item a" }, new shoppingcartitem { count = 17, title = "item b" }, new shoppingcartitem { count = 100, title = "item c" }, }.tolist() }; homecoming view(model); } [httppost] public actionresult index(shoppingcart model) { homecoming view(model); } }

view:

@model shoppingcart @{ layout = null; } <!doctype html> <html> <head> <title>index</title> </head> <body> @using (html.beginform()) { <fieldset> <legend>shoppingcart</legend> @html.textboxfor(x => x.couponcode) <hr/> @html.editorfor(x => x.items) </fieldset> <p> <input type="submit" value="save" /> </p> } </body> </html>

and editor template (~/views/home/editortemplates/shoppingcartitem.cshtml) rendered each item of items collection:

@model shoppingcartitem @html.editorfor(x => x.title) @html.editorfor(x => x.count)

.net vb.net asp.net-mvc-3 razor model-binding

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -