如何通过mvc3中的ajax调用部分视图

时间:2022-10-08 13:46:32

I need to call a partial view through ajax. I have tried the following, but I am not sure how to complete it.

我需要通过ajax调用部分视图。我尝试了以下,但我不知道如何完成它。

$("#UserName").change(function () {
        var userid = $("#UserName").val();
        var ProvincialStateID = $("#State").val();
        var Hobbyid = $("#Hobby").val();
        var Districtid = $("#DistrictNames").val();
        var Homeid = $("#Hobbyhome_EstablishmentId").val();
        var urlperson = '@Url.Action("FetchPersonByUserName")';
        $.ajax({
            type: "POST",
            url: urlperson,
            data: { userid: userid, stateid: ProvincialStateID, hobbyid: Hobbyid, districtid: Districtid, homeid: Homeid },
            success: function (data) { 
           //Dont know what to write here
        });
    });

Here is the function that I have written in my Controller:

这是我在Controller中编写的函数:

 [HttpPost]
    public ActionResult FetchPersonByUserName(int userid,int stateid,int districtid,int homeid,int Hobbyid)
    {
      //Code to fetch the data in the partial using all parameters
      return PartialView("_LearnerAssociationGridPartial", list);
    }

When I click on a dropdown the ajax gets called and I want the function which is called through ajax to redirect it to the partial view. Please help me because currently I am not able to display my partial view

当我点击一个下拉列表时,会调用ajax,我希望通过ajax调用的函数将其重定向到局部视图。请帮助我,因为目前我无法显示我的局部视图

2 个解决方案

#1


4  

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/

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.

更新对于nhibernate运行缓慢,请尝试http://www.hibernatingrhinos.com/products/nhprof这是nhibernate profiler,对于付费版本,或者尝试使用sql profiler查看查询执行的内容,通常你可以获得更多由于查询的复杂性,会期望和/或真正减慢查询。

#2


3  

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

我不明白你的意思是重定向到parial视图。通常人们使用ajax和部分视图来获取页面的一部分而不刷新页面(你可能已经在这个网站/ facebook / twitter等中看到过这种行为。)所以我想你可能想要显示你正在获取的数据异步显示在当前页面的一部分中。你可以在成功处理程序中做到这一点

$.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.

假设您当前页面中有一个id为divUserInfo的div。

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

如果你真的想在ajax帖子之后重定向,你可以这样做。

$.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.

就个人而言,我不使用HttpPost(在客户端和服务器上)如果它是一个获取一些数据的方法。我简单地使用jquery get或load。

$.get("yourUrl", { userid: userid, stateid: ProvincialStateID } ,function(data){
  $("#divUserInfo".html(data);
});

#1


4  

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/

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.

更新对于nhibernate运行缓慢,请尝试http://www.hibernatingrhinos.com/products/nhprof这是nhibernate profiler,对于付费版本,或者尝试使用sql profiler查看查询执行的内容,通常你可以获得更多由于查询的复杂性,会期望和/或真正减慢查询。

#2


3  

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

我不明白你的意思是重定向到parial视图。通常人们使用ajax和部分视图来获取页面的一部分而不刷新页面(你可能已经在这个网站/ facebook / twitter等中看到过这种行为。)所以我想你可能想要显示你正在获取的数据异步显示在当前页面的一部分中。你可以在成功处理程序中做到这一点

$.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.

假设您当前页面中有一个id为divUserInfo的div。

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

如果你真的想在ajax帖子之后重定向,你可以这样做。

$.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.

就个人而言,我不使用HttpPost(在客户端和服务器上)如果它是一个获取一些数据的方法。我简单地使用jquery get或load。

$.get("yourUrl", { userid: userid, stateid: ProvincialStateID } ,function(data){
  $("#divUserInfo".html(data);
});