How to call partial view through ajax in mvc3

不羁的心 提交于 2019-12-05 13:25:36

What you need is something like

$.ajax({
   type: "POST",
   url: urlperson,
   data: { userid: userid, 
           stateid: ProvincialStateID, 
           hobbyid: Hobbyid, 
           districtid: Districtid, 
           homeid: Homeid },
    success: function (data) { 
          var result = data; 
          $('targetLocation').html(result);
    }
   });

it is recomended to not use data straight from variable but you can. Now target location is where you want the result to be displayed to.

See more information in here:

http://api.jquery.com/jQuery.ajax/

As to slow fetching data, try optimalize your query

Update For nhibernate running slow, try http://www.hibernatingrhinos.com/products/nhprof which is nhibernate profiler, for paid version, or try sql profiler to see what is query is beeing executed, often you can get much more that you would expect and or really slow query due to complexity of the query.

I dont understand what you mean by redirect to the parial view. Usually people use ajax and Partial views to get part of a page without a page refresh ( you might have seen this kind of behaviour in this site/facebook/twitter etc..) So i guess you probably want to show the data you are fetching asynchronosly to be shown in a part of your current page. you can do that in your success handler

$.ajax({
        type: "POST",
        url: urlperson,
        data: { userid: userid, stateid: ProvincialStateID, hobbyid: Hobbyid, districtid: Districtid, homeid: Homeid },
        success: function (data) { 
          $("#divUserInfo".html(data);
        }
 });

Assumung you have a div with id divUserInfo in your current page.

If you really want to redirect after the ajax post, you can do it like this.

$.ajax({
        type: "POST",
        url: urlperson,
        data: { userid: userid, stateid: ProvincialStateID, hobbyid: Hobbyid, districtid: Districtid, homeid: Homeid },
        success: function (data) { 
          window.location.href="Why-I-need-Ajax-Then.php";
        }
 });

Personally, I dont use HttpPost (both in client and server) If it is a method to GET some data. I simpy use the jquery get or load.

$.get("yourUrl", { userid: userid, stateid: ProvincialStateID } ,function(data){
  $("#divUserInfo".html(data);
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!