使用id从数据库中检索数据并在mvc中以视图显示它

时间:2013-04-23 08:16:26

标签: asp.net-mvc listview asp.net-mvc-4 ado.net-entity-data-model

我的问题在于......请给我一个适合我的数据库和行动的新视图。

我的详细信息表:

Details table

使用html操作链接传递请求ID:

 @Html.ActionLink(item.Request_ID, "Details",new { requestid = item.Request_ID },null )

点击链接,我们应该获得与数据库链接相对应的详细信息。

行动方法:

public ActionResult Details(string requestid)
    {
        var entities = new EmployDBEntities1();
       var detailsModel = entities.Details.Single(e => e.Id == requestid);
       return View(detailsModel);
        //return View(entities.Details.ToList());
    }

希望我的问题是返回视图和设计视图。我的要求是我想要特定id的详细信息,并应在下面设计的视图中显示它们。我能够检查var details模型中的id,然后我必须从数据库中读取剩余的字段并显示我视图中的字段。我很明显这样做。请帮帮我。

查看:

model IEnumerable<Approvals.Models.Detail>

@{
ViewBag.Title = "Details";
//Layout = "~/Views/Shared/_Layout.cshtml";
}

@section Header {
@Html.ActionLink("Back", "PendingRequests", "Account", null, new { data_icon = "arrow-l", data_rel = "back" })
<h1>@ViewBag.Title</h1>
@Html.ActionLink("Log Off", "LogOff") 


}

<head>
 <link href="~/StyleSheet1.css" rel="stylesheet" type="text/css" />
    </head>
<div data-role="collapsible"  data-theme="b" data-content-theme="b">
    <h3>Employee Details</h3>
    <table class="td3">
        @foreach (var item in Model) {
        <tr>
            <td>Employee ID</td>
            <td>@Html.Encode(item.EmpID)</td>
        </tr>
            <tr>
                <td>Short ID</td>
                <td>
                    @Html.Encode(item.ShortID)
                </td>
            </tr>
            <tr>
            <td>Grade</td>
            <td>@Html.Encode(item.Grade)</td>
        </tr>
            <tr>
            <td>Vertical</td>
            <td>@Html.Encode(item.Vertical)</td>
        </tr>
            <tr>
            <td>Vertical Head</td>
            <td>@Html.Encode(item.VerticalHead)</td>
        </tr>
            <tr>
            <td>L1 Manager</td>
            <td>@Html.Encode(item.L1_Manager)</td>
        </tr>
            <tr>
            <td>L2 Manager</td>
            <td>@Html.Encode(item.L2_Mnager)</td>
        </tr>
            <tr>
            <td>CostCentre</td>
            <td>@Html.Encode(item.CostCentre)</td>
        </tr>
        }
    </table>
    </div>

2 个答案:

答案 0 :(得分:0)

  1. 而不是用于detailsModel对象的var类型,而是在“详细信息”操作中为类型提供绑定的类型,即IEnumerable中的Approvals.Models.Detail。

  2. 如果未在返回View()中提供,则视图名称应与操作名称匹配。否则返回“详细信息”操作中的视图(“xxx”,detailsModel)。

答案 1 :(得分:0)

    model Approvals.Models.Detail

@{
ViewBag.Title = "Details";
//Layout = "~/Views/Shared/_Layout.cshtml";
}

@section Header {
@Html.ActionLink("Back", "PendingRequests", "Account", null, new { data_icon = "arrow-l", data_rel = "back" })
<h1>@ViewBag.Title</h1>
@Html.ActionLink("Log Off", "LogOff") 


}

<head>
 <link href="~/StyleSheet1.css" rel="stylesheet" type="text/css" />
    </head>
<div data-role="collapsible"  data-theme="b" data-content-theme="b">
    <h3>Employee Details</h3>
    <table class="td3">
        <tr>
            <td>Employee ID</td>
            <td>@Html.Encode(Model.EmpID)</td>
        </tr>
            <tr>
                <td>Short ID</td>
                <td>
                    @Html.Encode(Model.ShortID)
                </td>
            </tr>
            <tr>
            <td>Grade</td>
            <td>@Html.Encode(Model.Grade)</td>
        </tr>
            <tr>
            <td>Vertical</td>
            <td>@Html.Encode(Model.Vertical)</td>
        </tr>
            <tr>
            <td>Vertical Head</td>
            <td>@Html.Encode(Model.VerticalHead)</td>
        </tr>
            <tr>
            <td>L1 Manager</td>
            <td>@Html.Encode(Model.L1_Manager)</td>
        </tr>
            <tr>
            <td>L2 Manager</td>
            <td>@Html.Encode(Model.L2_Mnager)</td>
        </tr>
            <tr>
            <td>CostCentre</td>
            <td>@Html.Encode(Model.CostCentre)</td>
        </tr>
    </table>
    </div>