In MVC ViewData pass objects to view the notes C #

I. Objectives

Learn how to say ViewData a List <Model> spread to the front desk in C # MVC development model, and the front desk how to read out the data cycle

Second, the platform

vs2013, MVC pattern

Third, an important part of the controller code shows

 #region 5.PC详情页
        public ActionResult PcDetail()
        {
            string pcUID = Request["id"].Trim();
            if (pcUID!=null || pcUID !="")
            {
                //PC详情数据
                PcBLL pcbll = new PcBLL();
                PcModel pm = new PcModel();
                pm = pcbll.getPcByUID(pcUID);
                //PC配件<CPU>数据
                List<PcPart> pp = new List<PcPart>();
                pp= pcbll.getPcPartByPcID_class(pcUID);
                ViewData["pcPart"] = pp;
                return View(pm);
            }
            return Content("没有这样的数据");
        }
#endregion

Fourth, the front end view wording

@model System007_Entity.PcModel
@using System007_Entity
<!DOCTYPE html>
<html>
<head>
<title>PC列表页</title>
</head>
<body>
<!--省略掉了N多与本篇文章无关的代码-->
 <div class="col-md-4">
            <strong>保修期开始:</strong><em>@Model.repairStart</em>
        </div>
        <div class="col-md-4">
            <strong>保修期结束:</strong><em>@Model.repairEnd</em>
        </div>
        <div class="col-md-4">
            <strong>创建人:</strong><em>@Model.createPerson</em>
        </div>
        <div class="col-md-4">
            <strong>创建时间:</strong><em>@Model.createTime</em>
        </div>
        <div class="col-md-4">
            <strong>编辑时间:</strong><em>@Model.editTime</em>
        </div>
        <div class="col-md-4">
            <strong>总备注:</strong><em>@Model.note</em>
        </div>
        <!--详情页加载结束-->
        <div class="row">

        </div>
        <hr />
        <h2>配件部分</h2>
        <div class="col-md-12">
            <table border="1">
                <tr>
                    <th>配件类别</th>
                    <th>配件品牌</th>
                    <th>配件型号</th>
                    <th>配件规格</th>
                    <th>配件价格</th>
                    <th>保修期开始</th>
                    <th>保修期结束</th>
                    <th>修改日期</th>
                    <th>创建日期</th>
                    <th>配件备注</th>
                </tr>
                @{
                    var pcParts = ViewData["pcPart"] as List<PcPart>;
                    if (pcParts != null && pcParts.Any())
                    {
                        foreach (var item in pcParts)
                        {
                            <tr>
                                <td>@item.pcPartClass</td>
                                <td>@item.brand</td>
                                <td>@item.xModel</td>
                                <td>@item.specification</td>
                                <td>@item.price</td>
                                <td>@item.repairStart</td>
                                <td>@item.repairEnd</td>
                                <td>@item.editTime</td>
                                <td>@item.createTime</td>
                                <td>@item.Note</td>
                            </tr>
                        }
                    }
                }
            </table>
</body>
</html>

V. code analysis:

1. The write controller ViewData [ "pcPart"], used to hold in List <Model>.

2. View first class references @using reference entity (a reference entity class can only, without reference to a specific object name)

3. html view to show the data write area @ {}, and then converted into the console defined ViewData List <Model> for recycle.

As examples: var pcParts = ViewData [ "pcPart"] as List <PcPart>; is received is converted into a viewdata listModel, for us to use foreach loop.

 

Measured effective. kahn 2019 Nian 7 Yue 2 Ri 16:55:06

Guess you like

Origin blog.csdn.net/xoofly/article/details/94454952