asp.net - C# MVC5 @Html.EnumDropDownListFor loses selection on "postback" -


mvc5 ef6

i have product. product can have multiple titles, title has type enum.

i working on create view product - model product

view:

            @for (int x = 0; x < model.prodtitles.count; x++)             {                                     <tr>                     <td>                         @html.textboxfor(model => model.prodtitles.toarray()[x].title, new { @class = "form-control" })                         @html.validationmessagefor(model => model.prodtitles.toarray()[x].title, "", new { @class = "text-danger" })                     </td>                     <td>                         @html.enumdropdownlistfor(model => model.prodtitles.toarray()[x].titletypeid, new { @class = "form-control" })                     </td>                     <td>                         @html.enumdropdownlistfor(model => model.prodtitles.toarray()[x].cultureid, new { @class = "form-control" })                     </td>                 </tr>             } 

in controller - when create product return view, create 1 title each title type , add product. view displays expect.

working required

when hit create button, product , titles returned controller expected , validate titles (different validation depending on type). add errors modelstate , therefore, modelstate.isvalid false.

i return view return view(product); debugging product, titles in product , still have correct types view displays first enum in list, titles , not 1 in model!

showing first enum titles

if change enumdropdown text box, correct type displayed, model correct:

proves model has correct type

i'm not sure why happening , hope can suggest fix? bug in enumdropdownfor? or doing wrong?

controller code:

    public actionresult create()     {         product product = new product();          foreach (var enm in utils.enums.enumhelper.getvalues<utils.enums.titletype>())         {             product.prodtitles.add(new prodtitle()             {                 cultureid = utils.enums.cultureid.english_united_kingdom,                                     datecreated = datetime.now,                 title = "",                 titletypeid = enm             });         }          return view(product);     }       [httppost]     [validateantiforgerytoken]     public actionresult create([bind(include = "prodid,datecreated")] product product, icollection<prodtitle> prodtitles)     {         //ensure titles valid before saving         (int x = 0; x < prodtitles.count; x++)         {             prodtitle title = prodtitles.toarray()[x];             if (!title.isvalid)             {                 modelstate.addmodelerror(string.empty, title.titletypeid + " title invalid.");             }             product.prodtitles.add(title);         }           if (modelstate.isvalid)         {             db.products.add(product);             db.savechanges();             return redirecttoaction("index");         }          return view(product);     } 

prodtitle model

public partial class prodtitle {     public long titleid { get; set; }     public long prodid { get; set; }     public utils.enums.titletype titletypeid { get; set; }     public string title { get; set; }     public utils.enums.cultureid cultureid { get; set; }     public system.datetime datecreated { get; set; }      public virtual product product { get; set; }     public virtual datasource datasource { get; set; } } 

when dealing dropdownlist in collection need custom editortemplate.

in /views/shared/editortemplates/prodtitle.cshtml

@model yourassembly.prodtitle  <tr>   <td>     @html.textboxfor(m => m.title, new { @class = "form-control" })     @html.validationmessagefor(m => m.title, new { @class = "text-danger" })   </td>   <td>     @html.enumdropdownlistfor(m => m.titletypeid, new { @class = "form-control" })   </td>   <td>     @html.enumdropdownlistfor(m => m.cultureid, new { @class = "form-control" })   </td> </tr> 

then in main view

@model yourassembly.product @using(html.beginform()) {   .... // other controls properties of product   @html.editorfor(m => m.prodtitles) // not in loop! } 

and modify controller

public actionresult create(product product) 

note: current [bind] attributes excludes prodtitle property binding, , in case should using view models represent want display/edit


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -