【问题标题】:Partial view won't update after ajax.beginformajax.beginform 后部分视图不会更新
【发布时间】:2013-10-30 13:50:02
【问题描述】:

我对 mvc3 还很陌生,所以请多多包涵。

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

@model List<BlokCWebwinkelGroep3.Models.Product>

在迷你菜单中,我有一个滑块(带有 2 个滑块手柄)和一个“搜索”按钮(两者都在 ajax.beginform 中)。当我单击搜索按钮时,滑块的 2 个值作为参数发送到 HttpPost 操作方法“搜索”(这些值在隐藏的输入字段中,当有人滑动滑块时由一些 javascript 更新),它返回一个部分视图(_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 "table-container"

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

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

部分视图

    <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 个是

我在这里做错了什么?

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

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

【问题讨论】:

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


    【解决方案1】:

    MSDN 文档指出 UpdateTargetId 更新 DOM Id,而不是您正在执行的类。将您的班级更改为 ID,它应该可以按预期工作。

    <div id="table-container"
    

    【讨论】:

    • 你是英雄,非常感谢!我认为这是一个编程太多的经典案例。我在代码的另一部分使用 ajax.actionlink 以正确的方式做到了这一点。太糟糕了,我不能投票! :(
    • 我的荣幸!有时很难只见树木不见森林:)。祝你好运
    猜你喜欢
    • 2013-01-25
    • 1970-01-01
    • 2016-11-09
    • 1970-01-01
    • 1970-01-01
    • 2020-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多