asp.net mvc - MVC 3 - In a controller how do you check which items are selected in the view? -
i have 3 models albums has instances of track , playlist has instances of track.
currently album view shows each track checkbox , dropdown @ bottom shows playlists user , add playlist button. users should able select tracks album , add them playlist have. i'm not sure controller handle this. view looks
**album view** @{ viewbag.title = "details"; } <h2>album: @model.title</h2> @{ list<sem_app.models.playlist> abc = viewbag.playlists; ienumerable<selectlistitem> selectlist = c in abc select new selectlistitem { text = c.playlistname, value = c.playlistname, }; } @using (html.beginform()){ foreach (var track in model.tracks) { @html.checkbox(track.title) @track.title <br /> } @html.dropdownlist("abd", selectlist) <input type="submit" value="add selected songs playlist" /> }
my controller method looks
[httppost] public actionresult details(formcollection fc) { return view(); }
how can check tracks user has selected , playlist user has selected in controller?
i'm assuming model album view @model album
.
your model be:
class track { .... .... bool isselected; }
and view:
@using (html.beginform()){ (int = 0; < model.tracks.count(); i++) { @html.checkboxfor(m => m.tracks[i].isselected) @model.tracks[i].name } }
so controller this:
[httppost] public actionresult details(album album, formcollection fc) { album.tracks;//should contain tracks album.tracks[0].isselected;//should return if selected fc["abd"];//should return element selected. return view(); }
your view needs html correctly rendered , values correctly bound controller.
you need use typed methods (ending for) bind other values controller. , want use @html.hiddenfor(m=>m.id)
create hidden field album know album posted back.
Comments
Post a Comment