如何将ID从主视图传递到另一个视图到局部视图

时间:2016-04-22 06:38:46

标签: sql-server-2008 asp.net-mvc-4

我有一个员工档案的主视图,其中只有关于员工的摘要或一般信息。我有一个详细信息选项,当单击时,将重定向到另一个视图(页面)。在那个页面上(我称之为详细信息页面),有一个部分视图w / c是一个表。

截至目前,部分视图显示w / c不应该是所有员工记录。它应该只检索从主视图中选择的员工的详细信息。

关于我如何做到这一点的任何建议?

这是主要观点

<table id="tbl_empProfile" class="table table-striped table-bordered table-hover dt-responsive nowrap" cellspacing="0">
    <thead>
        <tr>
           <th>@Html.DisplayNameFor(model => model.EMP_ID)</th>
           <th>@Html.DisplayNameFor(model => model.EMP_NUMBER)</th>
           <th> @Html.DisplayNameFor(model => model.FULL_NAME)</th>
           ....
        </tr>
    </thead>
    @foreach (var item in Model)
    {
         <tr>
             <td>@Html.DisplayFor(modelItem => item.EMP_ID)</td>
             <td>@Html.DisplayFor(modelItem => item.EMP_NUMBER)</td>
             <td>@Html.DisplayFor(modelItem => item.FULL_NAME)</td>
             ....
             <td>
                 @Html.ActionLink("Edit", "UpdateProfile", new { id = item.EMP_ID }) |
                 @Html.ActionLink("Details", "DetailsProfile", new { id = item.EMP_ID }) |
                 @Html.ActionLink("Delete", "Delete", new { id = item.EMP_ID })
             </td>
         </tr>
     }
 </table>

详情页面(另一个视图)

@model ebms_2.Models.EmpProfile
....
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    @Html.HiddenFor(model => model.EMP_ID)

    @Html.LabelFor(m => m.EMP_NUMBER, "Employee Number")
    @Html.TextBoxFor(model => model.EMP_NUMBER, new { @class = "form-control", @disabled = "disabled" })
    @Html.ValidationMessageFor(model => model.EMP_NUMBER)
    @Html.HiddenFor(x => x.EMP_NUMBER)

    @Html.LabelFor(m => m.FULL_NAME, "Employee Name")
    @Html.TextBoxFor(model => model.FULL_NAME, new { @class = "form-control", @disabled = "disabled" })
    @Html.ValidationMessageFor(model => model.FULL_NAME, "", new { @class = "text-danger" })
    @Html.HiddenFor(x => x.FULL_NAME)
}

<div class ="container">
    @Html.Partial("_EducationalBackground", Model.partialModel)
</div>

_Education_Background PARTIAL VIEW

@model IEnumerable<ebms_2.Models.EmpProfile>
@using ebms_2.Models.EmpProfile
@if (Model != null)
{
    <table id="tbl_empEduc" class="table table-striped table-bordered table-hover dt-responsive nowrap" cellspacing="0">
        <tr>
            <th>@Html.DisplayNameFor(model => model.EMP_ID)</th>
            <th>@Html.DisplayNameFor(model => model.SCHOOL_NAME)</th>
            <th>@Html.DisplayNameFor(model => model.YEAR_GRADUATED)</th>
            <th>@Html.DisplayNameFor(model => model.REMARKS)</th>
        </tr>
        @foreach (var item in Model)
        {
            <tr>
                <td>@Html.DisplayFor(modelItem => item.EMP_ID)</td>
                <td>@Html.DisplayFor(modelItem => item.SCHOOL_NAME)</td>
                <td>@Html.DisplayFor(modelItem => item.YEAR_GRADUATED)</td>
                <td>@Html.DisplayFor(modelItem => item.REMARKS)</td>
            </tr>
        }
    </table>
}

EmployeeProfileRepository

public List<EmpProfile> GetEmpEducation()
{
    connection();
    List<EmpProfile> empEducList = new List<EmpProfile>();
    SqlCommand com = new SqlCommand("SP_LIST_EMP_EDUCATION", con);
    com.CommandType = CommandType.StoredProcedure;      
    SqlDataAdapter da = new SqlDataAdapter(com);
    DataTable dt = new DataTable();
    con.Open();
    da.Fill(dt);
    con.Close();
    // Bind EmpModel generic list using dataRow
    foreach (DataRow dr in dt.Rows)
    {
        empEducList.Add(new EmpProfile
        {
            EMP_ID = Convert.ToInt32(dr["EMP_ID"]),
            SCHOOL_NAME = Convert.ToString(dr["SCHOOL_NAME"]),
            YEAR_GRADUATED = Convert.ToInt32(dr["YEAR_GRADUATED"]),
            REMARKS = Convert.ToString(dr["REMARKS"])
        });
    }
    return empEducList;
}

0 个答案:

没有答案