weixin_33725239 2014-08-12 06:59 采纳率: 0%
浏览 2

Ajax中的MVC Ajax

  • I have an @Ajax.BeginForm which updates a target "div1". In this div1, I have a table (a list of results of some sort).

  • Below the table, I have "div2". I want to update details in this div2 when a user clicks on a row in the table from div1. I want it to be Ajax-enabled, as well.

What would be the best approach?

  • 写回答

1条回答 默认 最新

  • weixin_33698043 2014-08-12 09:00
    关注

    Yes, something like this should do the trick:

    @using(Ajax.BeginForm("Action", new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "target-id" }))   
        {
    
        @Html.DropDownListFor(m => m.SomeField, new SelectList(Model.SomeList, "Id", "Name"))
    
        <div id="stuff-to-be-fetched">
    
        </div>
    
    ...............
    }
    
    <script>
     $('#SomeField').change(function () {
    
        var parameter = $(this).val();
        var url = "@Html.Raw(Url.Action("GetOtherList"))" + "?parameter=" + parameter;
    
        $.get(url, function (data) {
            $("#stuff-to-be-fetched").html(data);
        });
    });
    </script>
    

    New Action:

    public PartialViewResult GetOtherList(string parameter)
        {
            var list = _service.RetrieveList(parameter);
    
            return PartialView("_GetOtherList", list);
        }
    

    _GetOtherList.cshtml:

    @Html.Label("SomeOtherField)
    @Html.DropDownList("SomeOtherField", new MultiSelectList(Model, "Id", "Name")
    
    评论

报告相同问题?

悬赏问题

  • ¥20 python忆阻器数字识别
  • ¥15 无法输出helloworld
  • ¥15 高通uboot 打印ubi init err 22
  • ¥20 PDF元数据中的XMP媒体管理属性
  • ¥15 R语言中lasso回归报错
  • ¥15 网站突然不能访问了,上午还好好的
  • ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
  • ¥15 semrush,SEO,内嵌网站,api
  • ¥15 Stata:为什么reghdfe后的因变量没有被发现识别啊
  • ¥15 振荡电路,ADS仿真