简体   繁体   English

在ajax.beginform之后,部分视图将不会更新

[英]Partial view won't update after ajax.beginform

I'm fairly new to mvc3, so bear with me please. 我是mvc3的新手,所以请多多包涵。

This is the scenario: I have, inside my view, a small menu, and another partial view, which shows products whose names match the input from the user. 这是场景:在我的视图中有一个小菜单和另一个局部视图,该视图显示名称与用户输入匹配的产品。

@model List<BlokCWebwinkelGroep3.Models.Product>

In the mini menu I have a slider (with 2 slider handles) and a button 'search' (both are in a ajax.beginform). 在迷你菜单中,我有一个滑块(带有2个滑块手柄)和一个“搜索”按钮(均位于ajax.beginform中)。 when I click on the search button the 2 values of the slider get sent as parameters to a HttpPost actionmethod "search" (these values are in the hidden input field, which is updated by some javascript when someone slides the slider), which returns a partialview(_myPartial, model). 当我单击搜索按钮时,滑块的2个值将作为参数发送到HttpPost动作方法“搜索”(这些值在隐藏的输入字段中,当有人滑动滑块时,某些javascript会更新这些值),该返回值局部视图(_myPartial,模型)。 (the model is a list). (该模型是一个列表)。

Ajax.BeginForm Ajax.BeginForm

    @using (Ajax.BeginForm("Search",
                    new AjaxOptions
                    {
                        UpdateTargetId = "table-container",
                        InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace,
                        HttpMethod = "POST"
                    }))
{   
    <input type="hidden" name = "hidden" id="hidden_amount" />
    <div id="slider-range">
    </div>
    <br />
    <button type="submit" value="Search">
        Search</button>
}

Action Method 行动方法

    [HttpPost]
    public PartialViewResult Search(string hidden)
    {
        List<Product> Model = null;
        string[] values = hidden.Split(' ');
        int[] convertedString = Array.ConvertAll<string, int>(values, int.Parse);
        string name = (string)TempData["searched"];
        try
        {
            Model = ResultDBController.GetAdvancedSearched(name, convertedString[0], convertedString[1]);
        }
        catch (Exception e)
        {
            ViewBag.Foutmelding = "Er is iets foutgegaan: " + e;
        }
        return PartialView("_Product", Model);
    }

The UpdateTargetId is the div "table-container" UpdateTargetId是div“表容器”

    @*Calls the _Product partial view*@
<div class="table-container">@Html.Partial("_Product", Model)</div>

Then it runs through the partial view, however it does not show up on the screen.. 然后,它遍历部分视图,但是它不会显示在屏幕上。

Partial view 部分视图

    <table class="productlist">
@foreach (BlokCWebwinkelGroep3.Models.Product product in Model)
{ 
    <tr class="product" onclick="location.href = '@(Url.Action("ProductPage", "Product", new {ProductID = @product.ProductID}))'">

        <td>
            <img src = '@product.ImageURL' alt = '@product.Name' />
        </td>
        <td>
            @product.Name
        </td>
        <td>
            €@product.Price
        </td>
    </tr>
}

Say I search "er", that will display 3 products on screen. 假设我搜索“ er”,它将在屏幕上显示3个产品。 2 are < 75 euro the other one is 183 euro. 2个<75欧元,另一个是183欧元。 If I set the slider to 75 - 300, I can debug and see that everything is alright. 如果将滑块设置为75-300,则可以调试并看到一切正常。 The partialview which gets returned in the actionmethod contains only the product which is 183 euro, then it loops through the partial view once, however the partial view does not update to that version, it stays on the same version with the 3 products. 在操作方法中返回的partialview仅包含183欧元的产品,然后循环遍历部分视图,但是部分视图不会更新到该版本,它与3个产品保持相同的版本。

What am I doing wrong here? 我在这里做错了什么?

PS I use the following scripts in my header PS我在标头中使用以下脚本

        <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
<script type="text/css" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

MSDN documentation states that UpdateTargetId updates a DOM Id, not a class as you are doing. MSDN文档指出UpdateTargetId会更新DOM ID,而不是您正在执行的类。 Change your class to an ID and it should work as expected. 将您的班级更改为ID,它应该可以正常工作。

<div id="table-container"

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

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