asp.net mvc - ASP .NET MVC Ajax insert view instead instead replace data only first time -


i'm new ajax, , have simple example, has problem; first call data duplicated @ view , in subsequent calls work correctly. doing wrong?

the ~/views/shared/_layout.cshtml file had scripts necesary:

<script src="~/scripts/jquery-3.1.0.min.js"></script> <script src="~/scripts/jquery.unobtrusive-ajax.js"></script> 

first time, initial view:

firsttime

second time, first time button click inserts number instead of replace number:

enter image description here

third time, second time button click works fine in second line number... on:

enter image description here code: model

    namespace mqwebst.models {     public class ajaxtest     {         public int number { get; set; }     } } 

controller:

using system.web; using system.web.mvc; using mqwebst.models;  namespace mqwebst.controllers {     public class ajaxtestcontroller : controller     {         // get: ajaxtest         public actionresult vista()         {             ajaxtest @ = new ajaxtest { number = 1 };             return view(at);         }          [httppost]         public actionresult vista( ajaxtest model)         {             random rnd = new random();              model.number = rnd.next(1, 100);              return partialview("ajaxtestpartial", model);         }     } } 

view vista.chtml:

@model mqwebst.models.ajaxtest  @{      layout = "~/views/shared/_layout.cshtml";  }  @using (ajax.beginform("vista", new ajaxoptions { updatetargetid = "divemp", insertionmode = insertionmode.replace })) {     <button class="btn btn-primary btn-md glyphicon glyphicon-menu-right" name="contestar" type="submit" value="+1"></button>      <div class="panel panel-footer">         <table id="divemp">             @model.number.tostring()         </table>      </div>  } 

ajaxtestpartial.chtml:

@model mqwebst.models.ajaxtest @model.number.tostring() 

the issue you're using insertionmode = insertionmode.replace, it's going replace data in updatetargetid. use insertionmode.insertbefore instead , build values out in parent of #divemp. however, if need values placed in #divemp, you'll need write javascript handler onsuccess option of ajax form, way can dictate if you're doing insert vs. pre-pend vs. replace.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -