asp.net mvc - C# Entity Overview Table of one to many checkbox -
i can't seem find how build proper solution 1 many checkbox.
so have :
i have article , admin user can set user rights article. in article right page have overview users.
my article:
public partial class artikel { public artikel() { this.artikellinks = new hashset<artikellink>(); this.artikelrights = new hashset<artikelright>(); } public int id { get; set; } public string code { get; set; } public string naam { get; set; } public virtual icollection<artikellink> artikellinks { get; set; } public virtual icollection<artikelright> artikelrights { get; set; } }
my rights class
public partial class artikelright { public int id { get; set; } public system.guid userid { get; set; } public int artikelid { get; set; } public bool hasright { get; set; } public virtual artikel artikel { get; set; } }
how build view? tried several ways can't seem save data. rights view @ moment:
@using (html.beginform()) { <table width="90%" align="center" class="table table-striped"> <thead> <tr> <th align="left">gebruiker</th> <th align="left">heeft toegang</th> </tr> </thead> @html.editorfor(x => model.artikelrights, "artikelright") </table> <br /> <div class="pull-right"> <input type="submit" value="opslaan" class="btn btn-sm beige" /> </div> }
and partial artikel right view:
@model ienumerable<gtiproducts.models.artikelright> @foreach (var item in model) { <tr> <td> @membership.getuser(item.userid).username </td> <td> @html.editorfor(x => item.hasright) </td> </tr> }
my save action is:
public actionresult rights(artikel art) { repo.savechanges(); return view(art); }
when debug art.artikelrights null. how can fix , what's best solution entity framework?
rename partial make editortemplate
- /views/shared/editortemplates/artikelright.cshtml
, make following modifications
@model gtiproducts.models.artikelright // not ienumerable <tr> <td> @html.checkboxfor(m => m.hasright)</td> @html.labelfor(m => m.hasright, "....") // users name? @html.hiddenfor(m > m.id) // or ever property need identify artikelright.cshtml </td> </tr>
then in main view
<tbody> @html.editorfor(x => model.artikelrights) </tbody>
this correctly name controls indexers can bound model on post back, example
<input type="checkbox" name="artikelrights[0].hasright" ...> <input type="checkbox" name="artikelrights[1].hasright" ...>
side note: should not using @membership.getuser(item.userid).username
in view. instead create view model properties need in view (which appears id
, hasright
, , include property users display name (and populate in controller).
Comments
Post a Comment