C# MVC后台传值到前端并显示

C# MVC后台传值到前端并显示

一、通过ViewBag传值:传值可以是字段和list等其他类型

后台代码

            ViewBag.Message = "Your application description page.";
            ViewBag.Message2 = "Your application description page2.";

            ViewBag.Message3 = "Your application description page3.";

html页面获取代码:

        <h2>ViewBag</h2>
        <h3>@ViewBag.Title.</h3>
        <h3>@ViewBag.Message</h3>
        <h3>@ViewBag.Message2</h3>

        <h3>@ViewBag.Message3</h3>

二、通过ViewData:传值可以是字段和list等其他类型

后台代码:

           ViewData["mess1"] = "测试1";

           ViewData["mess2"] = "测试2";

            

            List<ZSKObj> zsk = new List<ZSKObj>();

            zsk.Add(new ZSKObj {ZSKDM=1, FLMC="分类1", LXMC="整改措施", MEMO="整改测试1" });
            zsk.Add(new ZSKObj { ZSKDM = 2, FLMC = "分类2", LXMC = "原因分析", MEMO = "原因分析测试1" });
            zsk.Add(new ZSKObj { ZSKDM = 3, FLMC = "分类1", LXMC = "整改措施", MEMO = "整改测试2" });

            ViewData["list"] = zsk;

HTML页面获取代码:

    <h2>ViewData</h2>
    <h3>@ViewData["mess1"]</h3>

    <h3>@ViewData["mess2"]</h3>

<table class="table table-striped table-bordered">
            <thead>
                <tr>
                    <th>类型</th>
                    <th>分类</th>
                    <th>明细</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in ViewData["list"] as List<Winning.NQCS.DAL.DataObject.Settings.ZSKObj>)
                {
                    <tr>
                        <td>@Html.DisplayFor(p => item.FLMC)</td>
                        <td>@Html.DisplayFor(p => item.LXMC)</td>
                        <td>@Html.DisplayFor(p => item.MEMO)</td>
                        <td>@Html.ActionLink("编辑", "UpdateZSK", "ZSK", new { zskdm = item.ZSKDM.ToString() }, null)                         </td>
                    </tr>
                }
            </tbody>

        </table>

三、通过TempData:传值可以是字段和list等其他类型,用法通ViewData

后台代码:

            TempData["Tmptest1"] = "Tmptest1";

            TempData["Tmptest2"] = "Tmptest2";

HTML页面获取代码:

    <h3>@TempData["Tmptest1"]</h3>

    <h3>@TempData["Tmptest2"]</h3>

四、直接在View中进行返回

后台代码:

            List<ZSKObj> zsk = new List<ZSKObj>();
            zsk.Add(new ZSKObj {ZSKDM=1, FLMC="分类1", LXMC="整改措施", MEMO="整改测试1" });
            zsk.Add(new ZSKObj { ZSKDM = 2, FLMC = "分类2", LXMC = "原因分析", MEMO = "原因分析测试1" });
            zsk.Add(new ZSKObj { ZSKDM = 3, FLMC = "分类1", LXMC = "整改措施", MEMO = "整改测试2" });
            ViewData["list"] = zsk;

            return View(zsk);

HTML页面获取代码:

@{

    ViewBag.Title = "About";
}
@model IEnumerable<Winning.NQCS.DAL.DataObject.Settings.ZSKObj>

<table class="table table-striped table-bordered">
            <thead>
                <tr>
                    <th>类型</th>
                    <th>分类</th>
                    <th>明细</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in ViewData["list"] as List<Winning.NQCS.DAL.DataObject.Settings.ZSKObj>)
                {
                    <tr>
                        <td>@Html.DisplayFor(p => item.FLMC)</td>
                        <td>@Html.DisplayFor(p => item.LXMC)</td>
                        <td>@Html.DisplayFor(p => item.MEMO)</td>
                        <td>@Html.ActionLink("编辑", "UpdateZSK", "ZSK", new { zskdm = item.ZSKDM.ToString() }, null) </td>
                    </tr>
                }
            </tbody>
        </table>

传递单个对象:

 public ActionResult Edit(string id)
        {
            List<UserInfoViewModel> dataList = HttpContext.Session["TestData"] as List<UserInfoViewModel>;
            UserInfoViewModel item = dataList.Find(p => p.UserID.ToString() == id);
            return View("Detail", item);

        }

html代码:

@model MVC5Demo1.Models.UserInfoViewModel

 @Html.HiddenFor(p => p.UserID)
        <div class="form-group">
            <label for="txtUserName" class="col-sm-2 control-label">姓名</label>
            <div class="col-sm-10">
                @Html.TextBoxFor(p => p.UserName, new { @class="form-control", placeholder = "姓名" })
            </div>

        </div>

五、参照网址:https://blog.csdn.net/fanbin168/article/details/39758291

posted @ 2021-01-06 16:13  不要摸我的腰  阅读(510)  评论(0编辑  收藏  举报