c# - Transferring info from view to Controller -


i'm working on asp-mvc application , facing following issue: have model simple properties plus 1 property list of custom object, , render ienumerable property mentioned here: passing ienumerable property of model controller post action- asp mvc

in view, have button supposed add items ienumerable property of model. of course, don't want lose inserted data, need pass model corresponding action.

i've noticed model os transferred entirely upon post. so, did like:

 $(".addbutton").click(function (event) {         event.preventdefault();         $("#filterform").submit();         @{ session["fromaddfullitem"] = "true";}         return false;       }); 

and in controller, like:

public actionresult index(filtermodel model)     {         if (session["fromaddfullitem"].tostring() == "true")         {             session["fromaddfullitem"] = "false";             return addblankitemtemplate(model);         } 

i've read assigning session in js not recommended, tried tempdata, , there data null.

my problem session["fromaddfullitem"] true, when come button. if put breakpoint in addbtn click in line- session["fromaddfullitem"] = "false";, , press other button, see odd reason mentioned breakpoint hit, though haven't pressed add button.

any help? maybe there way achieve want. currently, no matter button press (which posts form), comes session["fromaddfullitem"] = "false" , goes action addblankitemtemplate. thanks.

edit - ajax post

    $(".addbutton").click(function(event) {         event.preventdefault();         var modeldata = json.stringify(window.model);         $.ajax({             url:  '@url.action("addblankitemtemplate")',             type: 'post',             datatype: 'json',             data: modeldata,             contenttype: 'application/json; charset=utf-8',          });         return false;     }); 

and controller

public actionresult addblankitemtemplate(string modeldata) 

edit 2:

       $(".addbutton").click(function (event) {         event.preventdefault();         $.ajax({             url: '@url.action("addblankitemtemplate")',             data: $("#filterform").serialize()         }).success(function(partialview) {              $('detailstemplates').append(partialview);         });     }); 

and controller:

public actionresult addblankitemtemplate(filtermodel model) 

the line @{ session["fromaddfullitem"] = "true";} razor code , run on page rendering , load regardless of put in page.

it's not client side code won't wait js code run. if you're trying synchronise state between js , mvc have looked angularjs simplify these actions.


Comments