简体   繁体   English

使用Ajax的MVC部分视图-未出现在div中

[英]MVC Partial View with Ajax - Not appearing in div

I have a page that asks has a search by name function and then returns a partial view in a div with all the products that match the name. 我有一个页面,要求具有按名称搜索功能,然后在div中返回一个部分视图,其中包含与该名称匹配的所有产品。

This is my Ajax (details are being taken and passed to the controller accordingly since I tested it): 这是我的Ajax(自从我测试了细节以来,就将细节并相应地传递给控制器​​)​​:

 <script type="text/javascript">
 function SearchName() {
    var names = $('#prodname').val();

    var param = { name: names};

    $.ajax({
        url: '@Url.Action("FilternameM")',
        type: "POST",
        datatype: "html",
        UpdateTargetId: "divResult",
        data: param,
        success: function (data) {
            $('#divResult').empty();
            $('#divResult').html(data);
        }
    });
}
</script>

This is the controller (methods and variables are processed all properly): 这是控制器(方法和变量都已正确处理):

public PartialViewResult FilternameM(string name)
    {
        try
        {
            Product[] products = new ProductService.ProductsServiceClient().SearchProduct(name);
            //List<Product> prod = products.ToList();
            ViewBag.NameList = products;
            return PartialView("_filtername", products);
        }
        catch
        {
            return PartialView("_filtername");
        }
    }

The partial view is then below: 局部视图如下:

@model Common.Product[]

@foreach (var item in Model)
{
        <div id="item" class="itemStyleProd">  
        <br /> 
                   <img src="@Html.DisplayFor(modelItem => item.ImageLink)" width="100px" height="100px" alt="image" style="border:3px"/>
                  <br />                            
                    @Html.DisplayFor(modelItem => item.Name)    <br />      
                    Retail Price: €@Html.DisplayFor(modelItem => item.Price)<br />
                    Date Listed: @Html.DisplayFor(modelItem => item.DateListed) <br />
                    @Html.ActionLink("Details", "Details", new { id = item.Name })          

        </div>   
}

I debugged the function and it is entering both the controller method and the Partial View. 我调试了该函数,它同时进入了控制器方法和部分视图。 The only problem is that the partial view is not being displayed in the view then. 唯一的问题是,那时局部视图没有显示在视图中。 I think that it has to do with the ajax! 我认为这与ajax有关!

This is the div that should display it: 这是应该显示它的div:

<div id="divResult"></div>

Simply do like this: 只需这样做:

$.ajax({
        url: '@Url.Action("FilternameM")',
        type: "POST",
        datatype: "html",
        data: param,
        success: function (data) {
            $('#divResult').empty();
            $('#divResult').html(data);
        }

UpdateTargetId is Razor AjaxOptions class property, why are you putting it in jquery ajax. UpdateTargetId是Razor AjaxOptions类的属性,为什么将它放在jquery ajax中。

change the type of method "POST" to "Get" and try hope this will help you 将方法“ POST”的类型更改为“ Get”,并尝试希望对您有所帮助

$.ajax({
    url: '@Url.Action("FilternameM")',
    type: "GET",
    datatype: "json",
    data: param,
    success: function (data) {
        $('#divResult').empty();
        $('#divResult').html(data);
    }
});

That coding is actually CORRECT. 该编码实际上是正确的。 I just forgot to update my div name so it didn't match! 我只是忘记更新我的div名称,所以它不匹配!

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

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