使用 jquery Ajax 加载 PartialView?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11947540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:07:16  来源:igfitidea点击:

Load PartialView with using jquery Ajax?

asp.net-mvcjqueryhtml.dropdownlistfor

提问by AliR?za Ad?yah?i

PartialView

局部视图

@model OsosYeni23072012.Models.TblMeters
<h3>
    Model.customer_name
</h3>
<h3>
    Model.meter_name
</h3>

Controller

控制器

[HttpGet]
public ActionResult MeterInfoPartial(string meter_id)
{
    int _meter_id = Int32.Parse(meter_id);
    var _meter = entity.TblMeters.Where(x => x.sno == _meter_id).FirstOrDefault();

    return PartialView("MeterInfoPartial", _meter);
}

Razor

剃刀

@Html.DropDownList("sno", new SelectList(Model, "sno", "meter_name"), "-- Select Meter --", new { id = "meters"})

@Html.Partial("MeterInfoPartial")

I want to load partial view, if dropdownlist change. But I dont know How can I do this. I cant find any example about this. I do this with actionlink. But I did not with dropdown before.

如果下拉列表更改,我想加载部分视图。但我不知道我该怎么做。我找不到任何关于此的示例。我用 actionlink 来做到这一点。但我之前没有使用下拉菜单。

controller parameter meter_idequals dropdownlist selectedvalue.

控制器参数meter_id等于下拉列表选择值。

Thanks.

谢谢。

回答by Darin Dimitrov

You could subscribe to the .change()event of the dropdown and then trigger an AJAX request:

您可以订阅.change()下拉列表的事件,然后触发 AJAX 请求:

<script type="text/javascript">
    $(function() {
        $('#meters').change(function() {
            var meterId = $(this).val();
            if (meterId && meterId != '') {
                $.ajax({
                    url: '@Url.Action("MeterInfoPartial")',
                    type: 'GET',
                    cache: false,
                    data: { meter_id: meterId }
                }).done(function(result) {
                        $('#container').html(result);
                });
            }
        });
    });
</script>

and then you would wrap the partial with a div given an id:

然后你会用一个给定 id 的 div 包裹部分:

<div id="container">
    @Html.Partial("MeterInfoPartial")
</div>

Also why are you parsing in your controller action, leave this to the model binder:

另外,为什么要在控制器操作中进行解析,请将其留给模型绑定器:

[HttpGet]
public ActionResult MeterInfoPartial(int meter_id)
{
    var meter = entity.TblMeters.FirstOrDefault(x => x.sno == meter_id);
    return PartialView(meter);
}

Be careful with FirstOrDefaultbecause if it doesn't find a matching record in your database given the meter_idit will return nulland your partial will crash when you attempt to access the model.

小心,FirstOrDefault因为如果它没有在您的数据库中找到匹配的记录,meter_id它会返回null并且当您尝试访问模型时,您的部分将崩溃。

回答by ??????? ????????

<script type="text/javascript">
    $(function() {
        $('#addnews').click(function() {
                $.ajax({
                    url: '@Url.Action("AddNews", "Manage")',
                    type: 'GET',
                    cache: false,
                }).done(function(result){
                    $('#containera').html(result);
                });
        });
    });
</script>