c# - MVC 4: View is not rendered with the correct data from a List<> in the Model -


having pretty tricky asp.net mvc bug (using mvc4). i've tried strip down code make example clear possible

i have model

//this mvc model public class customcolumnconfiguration //stripped down "problem" property {     public customcolumnconfiguration()     {         columns = new list<columnconfig>();     }      public list<columnconfig> columns { get; set; } }   //mvc model contains list of these public class columnconfig {     public columnconfig()     {         name = "";         alias = "";         order = 0.0;     }      public string name { get; set; }     public string alias { get; set; }     public double order { get; set; }     public bool isenabled { get; set; } } 

here snippet razor view

<tbody> @for (int = 0; < model.columns.count; i++) {     <tr>         <td>column @(i+1)</td>         <td>@html.textboxfor(x => x.columns[i].name)</td>         <td>@html.textboxfor(x => x.columns[i].alias)</td>         <td>@html.textboxfor(x => x.columns[i].order)</td>         <td>@html.checkboxfor(x => x.columns[i].userange)</td>         <td><button name="removebtn@(i)" class="btn btn-danger submit-action" type="submit" value="remove-@(i)"><i class="icon-remove icon-white"></i></button></td>     </tr> } </tbody> //... @html.hidden("action") 

the following javascript responsible setting "action" input in form controller know index remove:

$(document).ready(function () {     $(".submit-action").on("click", function (e) {         var $this = $(this);         $("#action").val($this.val());     }); }); 

get , post controller code:

public actionresult managecustomcolumns() {     customcolumnconfiguration model = getconfiguration(); //this works           return view(model); }  [httppost]  public actionresult managecustomcolumns(customcolumnconfiguration model) {     var action = request.form["action"];     if (action.equals("saveindexconfiguration", stringcomparison.currentcultureignorecase))     {         this.alertsuccess("configuration saved");         setconfiguration(model); //this works     }     else if(action.startswith("remove",stringcomparison.currentcultureignorecase))     {         var indextoremove = int.parse(action.split('-')[1]);         model.columns.removeat(indextoremove);     }     else if (action.startswith("add", stringcomparison.currentcultureignorecase))     {         model.columns.insert(0, new columnconfig() { name = "new_column_name", alias = "new column alias" });     }      return view(model);  } 

if have page column configs 1, 2, 3, 4. can click "remove" button next column 2 , when debugging code in vs, see columnconfig name "2" in fact deleted model's columns property. when debugging razor, again, "2" column gone.

but, 100% consistency in ie , chrome, behavior i'm seeing 1,2,3 still there , deletes last 1 (4). not consistent @ i'm seeing in debugger. i'm not novice mvc has got 1 of magical bugs i've seen.

does have idea happening?

update furthermore, when click add, expect have row in html table things "new_column_name". add row, seems duplicate last row's data instead of putting values controller.

again, controller have 1 new columnconfig "new_column_name" in when debug, page gets rendered client has previous columnconfig's data in it. maddening!

i know old post had same problem .removeat() call. found solution. added code before .removeat() call.

modelstate.clear(); 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -