Friday, 15 March 2013

asp.net mvc 4 - Ajax.BeginForm doesn't work properly -



asp.net mvc 4 - Ajax.BeginForm doesn't work properly -

i have problem ajax submit. have main view render partialview , within lastly 1 load partialview. this:

main view

list of elements -> partialview 1

create new element -> partialview 2 within partialview 1

i using ajaxbeginform replace , update options:

@using (ajax.beginform("create", "mycontroller", new ajaxoptions { insertionmode = insertionmode.replace, updatetargetid = "form0", httpmethod = "post" }))

my problem submit works first time. saves new element , re-render partialview 1 ( updates list ). if want submit 1 time again redirect me partialview 1.

why happening , wrong in code? how can ?

here's controller action:

[httppost] public partialviewresult create(model viewmodel) { viewmodel.save(viewmodel.formmodel); var newviewmodel = new defaultviewmodel(viewmodel.xid,viewmodel.yid); homecoming partialview("_defaultpartialview", newviewmodel); }

it's ok action returns partialview? should of type jsonresult ?

and partialview 1:

@using (ajax.beginform("createbehaviorlog", "behaviorlog", new ajaxoptions { insertionmode = insertionmode.replace, updatetargetid = "form0", httpmethod = "post" })) { @model defaultviewmodel @scripts.render("~/bundles/jquery") <h2>title</h2> { html.renderpartial("partialview2", model.modelforpartialview2); } <div id="listofelements"> @foreach(var item in model.x) { --list-- } </div> }

thank you.

update:

i fixed ( newbie error ). i'll post tomorrow reply because it's kinda late , need sleep!

so, first of all, when create ajax phone call sure included need. here mean:

be sure have <add key="unobtrusivejavascriptenabled" value="true"/> in webconfig.

be sure have included script in page.

if using latest jquery need alter live function on in unobtrusive script.

btw, if have problem mentioned above here have done first time ( changed because it's not such solution ) :

mainview @using (ajax.beginform("create", "mycontroller", new ajaxoptions { insertionmode = insertionmode.replace, updatetargetid = "form0", httpmethod = "post" })) { renderpartial1 renderpartial1.2 } renderpartial2 .. renderpartialn

as can see, i've set ajaxbeginform outside partial1.2 submit, in mainview. method wasn't because if need 2 forms ?

in end quit using ajaxbeginform , used htmlbeginform ajax post javascript.

ajax asp.net-mvc-4

No comments:

Post a Comment