i have ef-model contains "key" , "value". value-table contains fk key. in ef-model looks this:
public partial class dict_key { public dict_key() { this.dict_value = new hashset<dict_value>(); } public int id { get; set; } public string name { get; set; } ... public virtual icollection<dict_value> dict_value { get; set; } //dict_value contains string "value" } my controller passing information editing this:
// get: keys/texts/5 [authorize] public async task<actionresult> texts(int? id) { var key = await db.dict_key .include(x => x.dict_value) .where(x => x.id.equals(id.value)) .firstordefaultasync(); return view(key); // debugging 'key' shows dict_value has 3 correct values. } this gets passed view shows dict_value's correct:
@model dict.models.dict_key @using (html.beginform()) { <div>key: @model.name </div> <table class="table"> <tr> <th>language</th> <th>text</th> </tr> @for (var = 0; < model.dict_value.count(); i++) { <tr> <td> @model.dict_value.elementat(i).dict_lang.name_en </td> <td> @html.editorfor(x => x.dict_value.elementat(i).value) </td> </tr> } <div class="form-group"> <input type="submit" value="save" /> </div> </table> } when submitting changes controller...
[httppost] public async task<actionresult> texts(dict_key dict_key) { if (modelstate.isvalid) { //also tried: db.entry(dict_key).state = entitystate.modified; db.entry(dict_key.dict_value).state = entitystate.modified; await db.savechangesasync(); return redirecttoaction("texts"); } return view(dict_key); } ..then "dict_key" totally different object passed edit-view. passed object contained collection of dict_value's , "returned" , edited object returns proper key object, empty dict_value collection.
i try avoid using userdefined model or viewbag of stuff manually. best practise solution this?
collection.elementat doesn't generate proper field name in razor. need list. here should use view model instead of entity directly , make dict_value collection list<dict_value> there.
alternatively, can create editor template dict_value , in view do:
@html.editorfor(m => m.dict_value) where dict_value there entire collection. razor render instance of editor template each member of collection , index everything.
Comments
Post a Comment