【问题标题】:Partial View returning shared layout page as well部分视图也返回共享布局页面
【发布时间】:2013-05-23 18:53:25
【问题描述】:

在我的 ASP MVC3 视图中,我使用以下 Ajax 调用来检索局部视图并将该局部视图附加到特定的 fieldset

Ajax

    $("#addItem").click(function () {
        alert("here");
        $.ajax({
            url: '@Url.Action("BlankDropDownItem", "DropDownValues")',
            dataType: 'html',
            cache: false,
            success: function (html) {
                alert(html);
                $("#items").append(html); 
            }
        });
        return false;
    });

这个Ajax 调用了一个非常简单的ViewResult 控制器方法,它应该返回部分视图。

控制器

    public ViewResult BlankDropDownItem()
    {
        return View("DropDownItemPartial", new DropDownValues());
    }

这是部分中的所有代码。当我在 VS 2010 中创建它时(我现在已经这样做了两次以确保)我检查了“部分视图”复选框,它使“使用共享布局”选项变灰。

局部视图

@model Monet.Models.DropDownValues
<div class="editor-label">
     @Html.LabelFor(model => model.AllowedValue)
</div>
<div class="label-field">
    @Html.EditorFor(model => model.AllowedValue)
    @Html.ValidationMessageFor(model => model.AllowedValue)
</div>
<div class="editor-label">
    @Html.LabelFor(model => model.DisplayValue)
</div>
<div class="label-field">
    @Html.EditorFor(model => model.DisplayValue)
    @Html.ValidationMessageFor(model => model.DisplayValue)
</div>

无论出于何种原因,当我在 Ajax success 函数的这一行返回的 html 对象上放置一个 alert

success: function (html) {

我看到html 对象从共享布局返回所有 HTML,因此它本质上是返回整个页面而不是部分视图。这是 Ajax 调用完成后的屏幕截图。

【问题讨论】:

    标签: ajax asp.net-mvc asp.net-mvc-3 jquery partial-views


    【解决方案1】:

    我认为您遇到了一个“容易错过”的小语法问题:D

    你有:

    public ActionResult BlankDropDownItem()
    {
        return View("DropDownItemPartial", new DropDownValues());
    }
    

    你应该有:

    public ActionResult BlankDropDownItem()
    {
        return PartialView("DropDownItemPartial", new DropDownValues());
    }
    

    希望这会有所帮助!

    【讨论】:

    • 太棒了,谢谢!仅供参考,它应该是 ActionResult 返回类型。它不会与ViewResult 一起编译。
    • 哈哈是的,应该的! :D 完全只是复制了你的文字。很高兴我能帮上忙!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多