如何通过AJAX在MVC3调用局部视图 [英] How to call partial view through ajax in mvc3

查看:169
本文介绍了如何通过AJAX在MVC3调用局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要调用通过AJAX的局部视图。我曾尝试以下,但我不知道如何去完成它。

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

$("#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:

 [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);
    }

当我点击下拉阿贾克斯被调用,我想它是通过AJAX调用重定向到局部视图功能。请帮助我,因为我目前无法显示我的局部视图

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({
   type: "POST",
   url: urlperson,
   data: { userid: userid, 
           stateid: ProvincialStateID, 
           hobbyid: Hobbyid, 
           districtid: Districtid, 
           homeid: Homeid },
    success: function (data) { 
          var result = data; 
          $('targetLocation').html(result);
    }
   });

这是recomended直从可变的,但你不能使用数据。 现在的目标位置是你想要的结果要显示给。

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.

在这里查看更多信息:

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

对于减缓获取数据,尝试optimalize查询

As to slow fetching data, try optimalize your query

更新 对于NHibernate的运行速度慢,请尝试 http://www.hibernatingrhinos.com/products/nhprof 这是NHibernate的探查,为付费版,或尝试SQL事件探查器,看看有什么是查询beeing执行,往往可以得到更多,你会期望和或很慢的查询由于查询的复杂性。

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.

这篇关于如何通过AJAX在MVC3调用局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆