使用$ .ajax时无法在视图中呈现局部视图

时间:2022-10-08 13:41:46

Not sure what I'm missing in my code. I have a view with a few radio buttons and want to render a different partial view when a radio button is selected. Here is my code:

不确定我的代码中缺少什么。我有一个带有几个单选按钮的视图,并且想要在选择单选按钮时呈现不同的局部视图。这是我的代码:

Controller

public ActionResult Method(string value)
        {
            var pv = "";
            switch (value)
            {
                case "radio1":
                    pv = "_XPartial";
                    break;

                case "radio2":
                    pv = "_YPartial";
                    break;

                case "radio3":
                    pv = "_ZPartial";
                    break;
            }
            return PartialView(pv);
        }

View div to render partialview

查看div以呈现partialview

    <div id"="renderarea">
        @*Render partialview here.*@

    </div>

JavaScript

$(document).ready(function () { GetPartial(); });

$("input[name='RadioOptions']").on('change', function () { GetPartial(); })

function GetPartial() {
    var selection = $("input[name='RadioOptions']:checked").val();
    //alert(selection) -- THIS ALERT SHOWS THE CORRECT VALUE
    $.ajax({
        url: '@Url.Action("Method", "Home")',
        data: {'value' : selection},
        contentType: 'application/html',
        type: 'GET',
        dataType: 'html',
        success: function (pv) {
            //alert(pv) -- THIS ALERT SHOWS THE HOLE PARTIAL VIEW HTML CODE
            $("#renderarea").html(pv); -- THIS HERE ISN'T WORKING
        }
    });
}

The part that seems to not be working is $("#renderarea").html(pv); and I really don't know why. Have someone had this issue before?

似乎不起作用的部分是$(“#renderarea”)。html(pv);我真的不知道为什么。以前有人有这个问题吗?

1 个解决方案

#1


0  

Try this, it should be work:

试试这个,它应该是工作:

    $.ajax({
    url: "/Home/Method",
    data: selection,       
    type: 'GET',
    dataType: 'html',
    success: function (pv) {
        $("#renderarea").html(pv); 
     }
    });

#1


0  

Try this, it should be work:

试试这个,它应该是工作:

    $.ajax({
    url: "/Home/Method",
    data: selection,       
    type: 'GET',
    dataType: 'html',
    success: function (pv) {
        $("#renderarea").html(pv); 
     }
    });