部分视图也返回共享布局页面

Nea*_*alR 2 ajax asp.net-mvc jquery partial-views asp.net-mvc-3

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

阿贾克斯

    $("#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;
    });
Run Code Online (Sandbox Code Playgroud)

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

调节器

    public ViewResult BlankDropDownItem()
    {
        return View("DropDownItemPartial", new DropDownValues());
    }
Run Code Online (Sandbox Code Playgroud)

这是部分中的所有代码.当我在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>
Run Code Online (Sandbox Code Playgroud)

无论出于何种原因,当我把一个alert在上html对象在此行中的Ajax的返回success功能

success: function (html) {
Run Code Online (Sandbox Code Playgroud)

我看到该html对象返回共享布局中的所有HTML,因此它基本上返回整个页面而不是局部视图.以下是Ajax调用完成后的外观屏幕截图.

在此输入图像描述

tec*_*osh 5

我认为你有一个小的"容易错过"的语法问题:D

你有:

public ActionResult BlankDropDownItem()
{
    return View("DropDownItemPartial", new DropDownValues());
}
Run Code Online (Sandbox Code Playgroud)

你应该有:

public ActionResult BlankDropDownItem()
{
    return PartialView("DropDownItemPartial", new DropDownValues());
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!