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

时间:2022-11-19 23:02:57

In my ASP MVC3 view I use the following Ajax call to retrieve a partial view and append the partial to a specific fieldset

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

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

This Ajax calls a very simple ViewResult controller method that is supposed to return the partial view.

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

Controller

调节器

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

This is all the code in the partial. When I created it in VS 2010 (I've done this twice now just to make sure) I checked the "Partial View" checkbox, which greys out the "Use Shared Layout" option.

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

Partial View

部分视图

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

For whatever reason, when I put an alert on the html object returned in this line of the Ajax success function

无论出于何种原因,当我在Ajax成功函数的这一行中返回的html对象上发出警报时

success: function (html) {

I see that the html object returns all the HTML from the shared layout, so it is essentially returning a whole page instead of the partial view. Here is a screen shot of what it looks like once the Ajax call is complete.

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

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

1 个解决方案

#1


5  

I think you are having one small "easy to miss" syntax issue :D

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

You have:

你有:

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

You should have:

你应该有:

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

Hope this helps!

希望这可以帮助!

#1


5  

I think you are having one small "easy to miss" syntax issue :D

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

You have:

你有:

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

You should have:

你应该有:

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

Hope this helps!

希望这可以帮助!