简体   繁体   English

Ajax不返回部分视图

[英]Ajax not returning Partial View

I reallly have a simple set of code to bring back a set of data that is triggered off a drop down. 我实际上只有一组简单的代码可以带回从下拉列表中触发的一组数据。

this is the script: 这是脚本:

function () {
   $('#ProviderID').change(function () {

    $.ajax({
        url: '/servicesDisplay/Index',
        type: 'Get',
        data: { id: $(this).attr('value') },
        success: function (result) {
            // The AJAX request succeeded and the result variable
            // will contain the partial HTML returned by the action
            // we inject it into the div:
            $('#serLocations').html(result);
        }
    });
}); 

This is the controller: 这是控制器:

 public ActionResult Index(string id)
 {
     int prid = Int32.Parse(id.Substring(0, (id.Length-1))); 
     string mulitval = id.Substring((id.Length-1), 1).ToString();
     System.Data.Objects.ObjectResult<getProviderServiceAddress_Result> proList = theEntities.getProviderServiceAddress(prid);
     List<getProviderServiceAddress_Result> objList = proList.ToList();
     SelectList providerList = new SelectList(objList, "AddressID","Address1");
     //ViewBag.providerList = providerList;
     return PartialView("servicesDisplay/Index", providerList);
 }

This is the view: 这是视图:

@model OCS_CS.Models.servicesDisplay

  <div>
    @Html.DropDownList(model => model.ServiceAdderssID, (IEnumerable<SelectListItem>)model)
 </div>   

When the drop down passes the in the value. 当下拉列表中的值通过时。 The apps does hit the controller. 这些应用程序确实命中了控制器。 But it highlightes the drop down in a light red and the view never displays. 但是它以浅红色突出显示下拉菜单,并且视图从不显示。

You are doing a GET, thats no meaning to pass data to ajax, you may pass data for POST: 您正在执行GET,那没什么意思将数据传递给ajax,您可以为POST传递数据:

First, put the value at the URL: 首先,将值放在URL:

function () {
 $('#ProviderID').change(function () {

 $.ajax({
    url: '/servicesDisplay/Index/' + $(this).attr('value'),
    type: 'Get',
    success: function (result) {
        // The AJAX request succeeded and the result variable
        // will contain the partial HTML returned by the action
        // we inject it into the div:
        $('#serLocations').html(result);
    }
  });
});

Second, mark the method as GET 其次,将方法标记为GET

 [HttpGet]
 public ActionResult Index(string id)

Hopes this help you! 希望这对您有所帮助!

Try this short version which uses the jquery load method. 尝试使用使用jquery load方法的简短版本。

$(function(){

   $('#ProviderID').change(function () {
      $('#serLocations').load("@Url.Action("Index","ServicesDisplay")?id="
                                                              +$(this).val());   
    });

}); 

If you want to avoid caching of result, you may send a unique timestamp along with the querystring to avoid caching. 如果要避免缓存结果,可以将唯一的时间戳记与查询字符串一起发送以避免缓存。

$('#serLocations').load("@Url.Action("Index","ServicesDisplay")?id="
                                                  +$(this).val()+"&t="+$.now()); 

You have quite a few problems with your code. 您的代码有很多问题。 First the model defined for your view is: 首先,为您的视图定义的模型是:

@model OCS_CS.Models.servicesDisplay

but in your action your're invoking the call to this view by passing in a SelectList: 但是在您的操作中,您通过传递SelectList来调用对此视图的调用:

SelectList providerList = new SelectList(objList, "AddressID","Address1");
return PartialView("servicesDisplay/Index", providerList);

this is not going to fly because the models do not match by type. 这不会成功,因为型号不匹配。 Seconds problem is you are casting this SelectList into an IEnumerable. 第二个问题是您要将这个SelectList转换为IEnumerable。 This is also not going to work. 这也行不通。 You need to cast to SelectList: 您需要强制转换为SelectList:

@Html.DropDownList(model => model.ServiceAdderssID, (SelectList)model)

but again until you match the type of your model in your action with the model on your view none of this will work. 但还是要等到您将操作中的模型类型与视图上的模型匹配时,以上所有方法均无效。 I suggest you install Fiddler to help you determine what sort of error are you getting. 我建议您安装Fiddler,以帮助您确定遇到哪种错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM