c# - 部分 View 不会在 ajax.beginform 之后更新

标签 c# javascript ajax asp.net-mvc-3 partial-views

我是 mvc3 的新手,所以请多多包涵。

这是场景: 在我的 View 中,我有一个小菜单和另一个局部 View ,它显示名称与用户输入匹配的产品。

@model List<BlokCWebwinkelGroep3.Models.Product>

在迷你菜单中,我有一个 slider (带有 2 个 slider handle )和一个“搜索”按钮(两者都在 ajax.beginform 中)。当我单击搜索按钮时, slider 的 2 个值作为参数发送到 HttpPost 操作方法“搜索”(这些值位于隐藏的输入字段中,当有人滑动 slider 时由一些 javascript 更新),它返回一个局部 View (_myPartial,模型)。 (模型是一个列表)。

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>
}

操作方法

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

UpdateTargetId 是 div“表容器”

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

然后它通过局部 View 运行,但是它没有显示在屏幕上..

局部 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>
}

假设我搜索“er”,屏幕上将显示 3 个产品。 2 个 < 75 欧元,另一个是 183 欧元。如果我将 slider 设置为 75 - 300,我可以调试并看到一切正常。在 action 方法中返回的局部 View 仅包含 183 欧元的产品,然后循环局部 View 一次,但是局部 View 不会更新到该版本,它与 3 个产品保持相同的版本。

我在这里做错了什么?

附言 我在标题中使用以下脚本

        <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文档指出 UpdateTargetId 更新了一个 DOM Id,而不是你正在做的一个类。将您的类(class)更改为一个 ID,它应该会按预期工作。

<div id="table-container"

关于c# - 部分 View 不会在 ajax.beginform 之后更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19684119/

相关文章:

javascript - 如何解析下面的简单 json 响应?

javascript - 具有 ISO 8601 格式的 JSON 数据的 D3 线图

javascript - 了解异步 javascript

javascript - 为什么将数据传递到 AJAX 请求 'success' 函数中?

python - 在 SQLAlchemy 的 session.query 中使用 like 运算符和 request.args.get

c# - 在 .NET 中跨 AppDomains 移动对象

c# - 如何检查是否已过期

c# - 使用 c# 创建新电子邮件并附加文件 Outlook 2016

c# - WPF: MemoryStream 占用大量内存

javascript - 在 Jquery 和 PHP 中实现轮询时,Ajax 不会触发