c# 4.0 - how to add object to a list in a view? -
i have typed view have form. display values of contact (object) in textboxes. contact has list of functions. have list of functions exist in database. in view i'm listing functions displaying checkboxes (value : id of function, display : name of function). before that, compare list of contact's functions functions , checked of contact. :
@foreach (extranetclient.models.classes.fonctioncontact fonction in viewbag.fonctions) { string coche = ""; if ((@model.listefonctions).where(c => c.idfonction == fonction.idfonction).count() > 0) { coche = "checked"; } <input type="checkbox" @coche id="@fonction.idfonction" />@fonction.libellefonction <br /> }
it looks that:
but now, if user checks checkbox add function contact, need save in contact's list. cannot find how that. has idea ?
first need modify checkboxes post properly. when checkbox checked posted name=value
. want checkboxes posted collection, name follow format function[x]
x index of checkbox. value becomes id of function.
@{ var = 0; } @foreach (extranetclient.models.classes.fonctioncontact fonction in viewbag.fonctions) { string coche = ""; if ((@model.listefonctions).any(c => c.idfonction == fonction.idfonction)) { coche = "checked"; } <input type="checkbox" @coche id="functions[@(i++)]" value="@fonction.idfonction" />@fonction.libellefonction <br /> }
now create action method can receive checkboxes's data. provide parameter can recieve enumeration of int
assuming function ids int
s.
public actionresult updatefunctions(ienumerable<int> functions) { // todo: functions contains ids of checked boxes. }
hope helps.
Comments
Post a Comment