asp.net mvc - @Ajax.ActionLink post form fields -
i have following form adding items list , updating resulting table. here code have:
view (strongly typed):
@using (ajax.beginform("addservice", "manager", new ajaxoptions { updatetargetid = "servicepartial", httpmethod = "post", loadingelementid = "loading" })) { <input type="submit" value="add new" /> <div class="widget-content table-container" id="servicepartial"> @html.partial("_services", model) </div> } partial:
@model project.model.servicesviewmodel <table id="demo-dtable-02" class="table table-striped"> <thead> <tr> <th>service</th> <th>price</th> <td class="action-col">actions</td> </tr> </thead> <tbody id="services"> @if (model != null) { if (model.services != null) { if (model.services.count > 0) { (int = 0; < model.services.count; i++) { <tr> <th>@model.services[i].name</th> <th>@model.services[i].price</th> <td class="action-col"> @html.hiddenfor(x=>x.services[i].name) @html.hiddenfor(x=>x.services[i].price) @html.hiddenfor(x=>x.services[i].serviceid) @ajax.actionlink(" x ", "appointment", "manager", new{ model = model, id = @model.services[i].serviceid }, new ajaxoptions { updatetargetid = "servicepartial", loadingelementid = "loading", httpmethod="post" }) </td> </tr> } } } } </tbody> </table> viewmodel:
public class servicesviewmodel { public list<service> services { get; set; } } right now, post through submit button of ajax.beginform works , i'm able post model , update services (add 1 it) , return it.
my problem able delete service list too. thought use ajax.actionlink , post id. problem returns id , not model.
now looking around apparently not possible send current model action using ajax.actionlink how guys handle this?
i thought clever , use overload of ajax.actionlink , add new{ model = model, id = @model.services[i].serviceid } no go.
there lot of other data in viewmodel (for space i've taken out of above code) , impractical me send these data 1 one through above overload.
you use form.serialize method post entire form action using ajax call. instead of using action link, try button. , in onclick event of button, serialize form , submit it.
$('#deletebutton').click(function(){ $.ajax( { type: "post", url:'@url.action("deleteactionname")', data: $('form').serialize(), success: function( response ) { console.log( response ); } } ); } );
Comments
Post a Comment