ajax
  • asp.net-mvc
  • 2013-04-15 35 views 1 likes 
    1

    所以我的方法中,我做了一些AJAX:有型的无ViewData的项目“的IEnumerable <SelectListItem>”具有关键“print_ad_option_id”

    function prodChange() { 
        console.log(this.value); 
    
        // ajax 
        var url = '@Url.Action("GetAdvProdList", "Contract")' + '?prod_id=' + this.value; 
        $.ajax({ 
         url: url, 
         type: 'GET', 
         success: showAdvProd, 
         error: function() { 
          console.log("FAILSHAKUREHKGAHH"); 
         } 
        }); 
    } 
    

    成功阿贾克斯的回报,并呼吁该控制器的方法。

    public ActionResult GetAdvProdList(int prod_id) { // product id 
         // get advertising products for this product 
         OutlookMediaEntities1 db = new OutlookMediaEntities1(); 
    
         var advsList = from printAdOption in db.print_ad_option 
             where printAdOption.print_product_id == prod_id 
             select printAdOption; 
    
         List<SelectListItem> adv_list = new List<SelectListItem>(); // list of ads 
         List<print_ad_option> advs = advsList.ToList(); // list from db 
         foreach (print_ad_option av in advs) 
         { 
          SelectListItem temp_item = new SelectListItem(); 
          temp_item.Text = av.name; 
          temp_item.Value = av.print_ad_option_id.ToString(); 
          adv_list.Add(temp_item); 
         } 
         ViewData["advproducts"] = new SelectList((IEnumerable<SelectListItem>)adv_list.ToList(), "Value", "Text"); 
    
         return null; 
        } 
    

    我返回null,因为,如果我返回查看或PartialView不工作,我不认为我希望它返回反正那些东西。我只想要设置viewdata的东西。

    在添加代码以使用视图数据之前,ajax运行良好。不幸的是,当我尝试在ajax成功方法中使用新的ViewData时,出现以下错误: “没有类型为'IEnumerable'的具有'print_ad_option_id'键的ViewData项” 事情是当我收到此错误时我第一次加载页面,所以它似乎是试图评估“@ Html.DropDownListFor ......”的showAdvProd函数被调用之前:

    // called when the ajax returns a list of adv prods in a viewdata 
    function showAdvProd() { 
        // add drop down 
        $("#drdn1").append('@Html.DropDownListFor(m => m.print_ad_option_id, ViewData["advproducts"] as SelectList)'); 
    } 
    

    ,做阿贾克斯函数被调用时,选定项目从一个(不同的)下拉菜单,但这个错误的页面甚至没有加载,所以显然ajax函数从来没有被调用,因此控制器方法从不调用。所以当然它不会识别viewdata ...

    我同样在我的窗体的另一部分创建一个下拉菜单,使用viewdata但没有ajax,它工作正常。所以我认为我的ajax或我的控制器方法有问题,而不是我如何使用viewdata。

    非常感谢您的帮助!

    回答

    0

    问题是,在您的初始页面加载时,视图引擎将评估所有代码块 - 任何在其前面具有'@'符号的东西。因此,它会在它存在之前尝试寻找名为副产品的ViewData项目,即使该参考位于尚未调用的JavaScript函数中。记住 - 代码块(例如@Html.blahblahblah()是服务器端的评估,而你期望你的AJAX在客户端运行,在页面发送到客户端之后。

    有几个选项:返回部分视图应该。工作你可以返回一个包含一个下拉列表中partialview从ViewBag填充这将是这样的:

    初始页:

    ... 
    <div id="advProductsSection"></div> 
    ... 
    

    你的AJAX需要调用相同的功能,但将返回局部视图:

    public ActionResult GetAdvProdList(int prod_id) { // product id 
    ... 
    ViewData["advproducts"] = new SelectList((IEnumerable<SelectListItem>)adv_list.ToList(), "Value", "Text"); 
    return View("_AdvProducts"); 
    } 
    

    然后局部视图(这里命名_AdvProducts.cshtml):

    @Html.DropDownList("className", (IEnumerable<SelectListItem>) ViewBag["advproducts"]) 
    

    你的AJAX功能需要改变,以更换新的部分股利:

    success: function (result) { 
        $('#advProductsSection').html(result); 
    } 
    

    或者类似的东西..

    第二种选择是从操作方法返回JSON并使用该方法填充下拉菜单。

    控制器:

    ... 
    return JSON(adv_list.ToList()); 
    } 
    

    然后在你的阿贾克斯成功FUNC:

    success: function(list) { 
        // states is your JSON array 
        var $dropdown = $('#Dropdown'); 
        $.each(list, function(i, product) { 
         $('<option>', { 
          value: list.Value 
         }).html(list.Text).appendTo($dropdown); 
        }); 
    } 
    

    你需要再次夹板下拉时prodchange被调用。

    +0

    谢谢,这使我走上了正确的轨道,我得到了它的工作。 – maus

    相关问题