asp.net - Ajax.Beginform + Partial view + Model state are not working together -
i have following create action method inside asp .net mvc :-
public actionresult createvmnetwork(int vmid) { vmassignips vmips = new vmassignips() { technologyip = new technologyip() { technologyid = vmid}, istmsipunique = true, istmsmacunique = true }; return partialview("_createvmnetwork",vmips); }
which render following partial view:-
@model tms.viewmodels.vmassignips @using (ajax.beginform("createvmnetwork", "virtualmachine", new ajaxoptions { insertionmode = insertionmode.insertafter, updatetargetid = "networktable", loadingelementid = "loadingimag", httpmethod= "post" })) { @html.validationsummary(true) @html.hiddenfor(model=>model.technologyip.technologyid) <div> <span class="f">ip address</span> @html.editorfor(model => model.technologyip.ipaddress) @html.validationmessagefor(model => model.technologyip.ipaddress) <input type="checkbox" name="istmsipunique" value="true" @(html.raw(model.istmsmacunique ? "checked=\"checked\"" : "")) /> | <span class="f"> mac address</span> @html.editorfor(model => model.technologyip.macaddress) @html.validationmessagefor(model => model.technologyip.macaddress) <input type="checkbox" name="istmsmacunique" value="true" @(html.raw(model.istmsmacunique ? "checked=\"checked\"" : "")) /> </div> <input type="submit" value="save" class="btn btn-primary"/> }
inside following main view, after clicking on ajax.actionlink:-
@ajax.actionlink("add network info", "createvmnetwork","virtualmachine", new { vmid = model.virtualmachine.tmsvirtualmachineid }, new ajaxoptions { insertionmode = insertionmode.replace, updatetargetid = "assignnetwork" , loadingelementid = "progress" } ) </p> <p><img src="~/content/ajax-loader-bar.gif" class="loadingimage" id="progress" /></p> <div id ="assignnetwork"></div>
then when clicking on “save
” button call following action method:-
[httppost] public actionresult createvmnetwork(vmassignips vmip) { if (modelstate.isvalid) { try { repository.insertorupdatevmips(vmip.technologyip,user.identity.name); repository.save(); return partialview("_networkrow",vmip); } catch (exception ex) { modelstate.addmodelerror(string.empty, "error occurred: " + ex.innerexception.message); }} return partialview("_createvmnetwork", vmip); }
when render following partial view _networkrow
:-
@model tms.viewmodels.vmassignips <tr id="@model.technologyip.id"> <td> @model.technologyip.ipaddress</td> <td>@model.technologyip.macaddress</td> <td>@ajax.actionlink("delete", "deletenetworkinfo", "virtualmachine", new { id = model.technologyip.id }, new ajaxoptions { confirm = "are sure want delete (" + model.technologyip.ipaddress + ")" + "( " + model.technologyip.macaddress + ").", httpmethod = "post", onsuccess = "deletionconfirmation", onfailure = "deletionerror" })</td> </tr>
all above work fine unless model state error or exception occurred , in case table updated partial view , model state displayed under table fields. need display model state error on same original view. need ajax.begin form update table if no exception or model state errors occured, , display error message inside original partial view, not under table.
can advice on how solve issue?
i don't question clearly, think should place <div>
view want show errors.
then, if got errors on processing, push error messages through viewbag
model.
so, action method become this:
[httppost] public actionresult createvmnetwork(vmassignips vmip) { if (modelstate.isvalid) { try { repository.insertorupdatevmips(vmip.technologyip,user.identity.name); repository.save(); return partialview("_networkrow",vmip); } catch (exception ex) { viewbag.errormessage = "some messages..."; // other info } } return partialview("_createvmnetwork", vmip); }
and in view this:
<div> <p>@viewbag.errormessage</p> </div>
so, if you got errors, they'll shown in
Comments
Post a Comment