jquery - I can not return to PartialView by using JSON -
i have been looking return partialview using json.however can see partial view's name html.
İf username , password not null , ı want redirect partialview.
[httpget] public actionresult index() { return view(); }
index view:
<script type="text/javascript"> function test() { var veri = { kullaniciad: $('#kullaniciad').val(), sifre: $('#sifre').val(), }; $.ajax({ url: "/home/menu", type: "post", datatype: "json", contenttype: 'application/json', data: json.stringify(veri), success: function (mydata) { $("#message").html(mydata); }, error: function () { $("#message").html("error"); } }); return false; } </script> <input type="text" id="kullaniciad" name="kullaniciad" /> <input type="password" id="sifre" name="sifre" /> <input type="button" onclick="test()" value="giriş" /> <div id="message"></div>
my menu actionresult
public actionresult menu(mymodel model) { if (model.kullaniciad != null && model.sifre != null) { return json("_menupartial", jsonrequestbehavior.allowget); } return null; }
you need render view string
public string renderrazorviewtostring(string viewname, object model) { viewdata.model = model; using (var sw = new stringwriter()) { var viewresult = viewengines.engines.findpartialview(controllercontext, viewname); var viewcontext = new viewcontext(controllercontext, viewresult.view, viewdata, tempdata, sw); viewresult.view.render(viewcontext, sw); viewresult.viewengine.releaseview(controllercontext, viewresult.view); return sw.getstringbuilder().tostring(); } }
in controller
var stringview = renderrazorviewtostring("_menupartial",model) return json(stringview , jsonrequestbehavior.allowget);
Comments
Post a Comment