zoukankan      html  css  js  c++  java
  • MVC 局部加载页面的实例

    我们在做MVC 进行某一块的局部刷新,有的使用AJAX 请求,有的使用局部页;

    下面我给大家推荐一种使用局部页面实现的这种方式:

    第一步:

    嵌套视图页

     <div id="showAudit">
            @*操作信息*@
            @{Html.RenderPartial("ShowAudit", Model);}       
        </div>
    View Code

    第二步:

    编写后台代码

    [HttpGet]
            public ActionResult ShowAudit(int id)
            {
                using (var _ctx = new BtDbContext())
                {
                    var memberSupply = _ctx.MemberSupplys.FirstOrDefault(c => c.MemberId == id);
    
                    MemberSupplyModel model = new MemberSupplyModel();
    
                    if (memberSupply != null)
                    {
    
                        model.MemberId          = memberSupply.MemberId;
                        model.OrderNumber       = memberSupply.OrderNumber;
                        model.CardId            = memberSupply.CardId;
                        model.Name              = memberSupply.Name;
                        model.MemberMobile      = memberSupply.MemberMobile;
    
                        model.State             = memberSupply.State;
                        model.CreateTime        = memberSupply.CreateTime;
                        model.UpdateTime        = memberSupply.UpdateTime;
                        model.CompanyName       = memberSupply.CompanyName;
                        model.CompanyAddress    = memberSupply.CompanyAddress;
    
                        model.CompanyPhone      = memberSupply.CompanyPhone;
                        model.CompanyUrl        = memberSupply.CompanyUrl;
                        model.ManagementProduct = memberSupply.ManagementProduct;
                        model.ContactJob        = memberSupply.ContactJob;
                        model.ContactPeople     = memberSupply.ContactPeople;
    
                        model.ContactMobile     = memberSupply.ContactMobile;
                        model.Memo              = memberSupply.Memo;
                        model.OpEmployeeName    = memberSupply.OpEmployeeName;
                    }
    
                    return PartialView(model);
                }
            }
    View Code

    第三步:

    编写视图页面:

    @using WechatMall.Common.Extensions;
    
    @model WechatMall.Model.ViewModel.MemberSupplyModel
    
    @{
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    @if (Model.State != WechatMall.Common.EnumHelper.MemberApplyForStatus.AuditNot)
    {
        
    
    <table class="ltable" style="margin-top:30px;">
        <thead>
            <tr>
                <th colspan="6" style=" text-align:left;padding-left:20px; font-weight:bold; background-color:#ffffff; ">操作信息</th>
            </tr>
        </thead>
        <tbody class="ltbody">
            <tr>
                <td style="18%">@Html.DisplayNameFor(model => model.OpEmployeeName)</td>
                <td style="18%">
                    @Html.DisplayFor(modelItem => Model.OpEmployeeName)
                </td>
    
                <td>@Html.DisplayNameFor(model => model.State)</td>
                <td style="15%">
                    @Model.State.ToDescriptionString()
                </td>
    
                <td>@Html.DisplayNameFor(model => model.UpdateTime)</td>
                <td>
                    @Html.DisplayFor(modelItem => Model.UpdateTime)
                </td>
            </tr>
            <tr>
                <td>@Html.DisplayNameFor(model => model.Memo)</td>
                <td colspan="5">
                    @Html.DisplayFor(modelItem => Model.Memo)
                </td>
            </tr>
        </tbody>
    </table>
    }
    View Code

    第四步:

    编写JS 代码

        $("#sbmbtn").click(function () {
    
                $.post('@Url.Action("AuditDispose", "Members")', $("#form1").serialize(), function (json) {
    
                    if (json.f) {
                        $("#showAudit").load('@Url.Action("ShowAudit", "Members", new { id = Model.MemberId }, Request.Url.Scheme)')   
                    }
                    else {
                        alert(json.ext);
                    }
                });
    
    
            });
    View Code

    这样就可以轻松搞定了;推荐一下参考网址:https://cmatskas.com/update-an-mvc-partial-view-with-ajax/

    那有里有不对的地方希望大家指出来,或者有更好的方法提出来,大家相互学习,进步..

  • 相关阅读:
    cfdem链接库地址不对的解决方法(liblmp_auto.so)
    总结入门学习OpenFOAM的资料(网址、论坛、帖子、博客等)
    运行cfdemCFDEMuti编译时出现的错误
    mapreduce 的三种测试方式
    Shell 编程
    hadoop集群搭建
    hadoop的环境配置
    hadoop 模板虚拟机环境准备以及对模板机的克隆
    linux总结
    解决maven控制台出现乱码情况
  • 原文地址:https://www.cnblogs.com/lizichao1991/p/5695477.html
Copyright © 2011-2022 走看看