jquery - PartialView and unobtrusive client validation not working -
i'm using asp.net mvc3 rc , i'm using unobtrusive jquery validations described brad wilson on his blog. works great when send form (in ajax) server, server side validations , return same row (that included in partial view) if model state isn't valid. 2 problems :
1st : when return partialview
in action, unobtrusive attributes aren't rendered. found "non elegant" way when it, client validations broken. after return action, if call jquery.validator.unobtrusive.parse()
on updated row, $("form").valid()
return true if it's not case.
2nd : want rendered view render string on server can send in jsonresult (ex:myjsonresult.html=renderpartialtostring("partialname",model)
).
has reference, there's view (editinvitation) :
<td> <%= html.hiddenfor(x=>x.id,new{id="id"}) %> <%= html.hiddenfor(x=>x.groupid,new{id="groupid"}) %> <%: html.textboxfor(x => x.name, new { id = "name" })%><%:html.validationmessagefor(x=>x.name) %> </td> <td> <%: html.textboxfor(x => x.email, new { id = "email" })%> <%:html.validationmessagefor(x=>x.email) %> </td> <td> <%: model.status.tofriendlyname()%> </td> <td> <%= invitationsviewmodel.renderactions(model, html, invitationsviewmodel.createrowid(model.id))%> </td>
and controller action :
if (tryupdatemodel(invitation)) { validmodel = true; //other stuff } if (request.isajaxrequest()) { //todo : return partial view prefer return jsonresult rendered view string in property of json result return partialview(validmodel ? "displayinvitation" : "editinvitation", invitation); }
thanks
i make worked. how :
htmlhelper helper = gethelper(); mvchtmlstring partialview = helper.partial("myview" , model); var result = new { success = modelstate.isvalid, html = partialview.tostring() }; return json(result);
there's helper functions:
protected htmlhelper gethelper() { return gethelper(string.empty); } protected htmlhelper gethelper(string formid) { htmlhelper helper = new htmlhelper(getviewcontext(formid), new viewpage { viewdata = this.viewdata }); helper.enableclientvalidation(isclientvalidationenabled()); helper.enableunobtrusivejavascript(isunobtrusivejavascriptenabled()); return helper; } private viewcontext getviewcontext(string formid) { var vc = new viewcontext(this.controllercontext, new webformview(this.controllercontext, "~/views/home/index.aspx"), this.viewdata, new tempdatadictionary(), new system.io.stringwriter()); vc.unobtrusivejavascriptenabled = isunobtrusivejavascriptenabled(); vc.clientvalidationenabled = isclientvalidationenabled(); vc.formcontext = new formcontext { formid = formid }; return vc; }
i'm not sure it's best way worked me. let's hope asp.net mvc team provide easier way render view string.
thanks
Comments
Post a Comment