C# MVC PartialView未填充模型详细信息

C# MVC PartialView未填充模型详细信息,c#,razor,model-binding,asp.net-mvc-partialview,C#,Razor,Model Binding,Asp.net Mvc Partialview,我打算在razor视图的左侧显示一个大约有五列的员工列表,在选择任何员工(行)时,在右侧以只读部分视图的形式显示该员工的详细信息,如下图所示。我希望稍后在另一个视图中重用详细信息表单以添加/更新记录。 我的控制器如下所示: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace Mymvc.Controllers {

我打算在razor视图的左侧显示一个大约有五列的员工列表,在选择任何员工(行)时,在右侧以只读部分视图的形式显示该员工的详细信息,如下图所示。我希望稍后在另一个视图中重用详细信息表单以添加/更新记录。

我的控制器如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Mymvc.Controllers
{
    public class EmpController : Controller
    {
        private readonly MyEntities _db = new MyEntities();
        private IEnumerable<vEmployee> _Employees;

        // GET: Employee
        public ActionResult Index()
        {
            ViewData["ModuleName"] = "Active Employees";
            ViewData["deptIDList"] = _db.lu_Department.OrderBy(e => e.Dept);
            ViewData["jobTitleIDList"] = _db.lu_JobTitle.OrderBy(e => e.JobTitle);
            ViewData["genderList"] = _db.lu_Gender.OrderBy(e => e.Gender);
            ViewData["ethnicityList"] = _db.lu_Ethnicity.OrderBy(e => e.Ethnicity);
            ViewData["nationalityList"] = _db.lu_Nationality.OrderBy(e => e.Nationality);
            ViewData["staffLocationList"] = _db.lu_StaffLocation.OrderBy(e => e.StaffLocation);
            ViewData["notchList"] = _db.lu_EmploymentGradeNotch.OrderBy(e => e.Notch);
            ViewData["salutationList"] = _db.lu_Salutation.OrderBy(e => e.Title);
            return View();
        }

        public ActionResult EmployeeDetails(int employeeID = 0)
        {
            vEmployee SelectedEmployee = null;
            ViewData["deptIDList"] = _db.lu_Department.OrderBy(e => e.Dept);
            ViewData["jobTitleIDList"] = _db.lu_JobTitle.OrderBy(e => e.JobTitle);
            ViewData["genderList"] = _db.lu_Gender.OrderBy(e => e.Gender);
            ViewData["ethnicityList"] = _db.lu_Ethnicity.OrderBy(e => e.Ethnicity);
            ViewData["nationalityList"] = _db.lu_Nationality.OrderBy(e => e.Nationality);
            ViewData["staffLocationList"] = _db.lu_StaffLocation.OrderBy(e => e.StaffLocation);
            ViewData["notchList"] = _db.lu_EmploymentGradeNotch.OrderBy(e => e.Notch);
            ViewData["salutationList"] = _db.lu_Salutation.OrderBy(e => e.Title);
            try
            {
                if (employeeID == 0)
                    employeeID = _db.Employees.OrderBy(m => m.EmployeeNumber).First().EmployeeID;

                if (_Employees == null)
                    _Employees = GetEmployees();

                SelectedEmployee = _Employees.First(e => e.EmployeeID == employeeID);

            }
            catch (Exception e)
            {
                var msg = e.Message;
            }
            return View(SelectedEmployee);
        }

        private IEnumerable<vEmployee> GetEmployees()
        {
            IEnumerable<vEmployee> emp = (from e in _db.Employees
                                          join c in _db.Contacts on e.EmployeeID equals c.EmployeeID
                                          join g in _db.lu_EmploymentGradeNotch on e.NotchID equals g.NotchID
                                          join u in _db.lu_Salutation on c.SalutationID equals u.TitleID into sal
                                          from u in sal.DefaultIfEmpty()
                                          join s in _db.lu_SUBDepartment on e.SubDeptID equals s.SubDeptID into sde
                                          from s in sde.DefaultIfEmpty()
                                          join l in _db.lu_StaffLocation on e.StaffLocationID equals l.StaffLocationID into cl
                                          from cm in cl.DefaultIfEmpty()
                                          join t in _db.lu_Ethnicity on c.EthnicityID equals t.EthnicityID into eth
                                          from et in eth.DefaultIfEmpty()
                                          join n in _db.lu_Nationality on c.NationalityID equals n.NationalityID into nt
                                          from nt1 in nt.DefaultIfEmpty()
                                          where c.ContactTypeID == 1 && e.EmploymentStatusID == 1
                                          select new vEmployee
                                          {
                                              EmployeeID = e.EmployeeID,
                                              EmployeeNumber = e.EmployeeNumber,
                                              DeptID = e.DeptID,
                                              SubDeptID = e.SubDeptID,
                                              JobTitleID = e.JobTitleID,
                                              ReportsToID = e.ReportsToID,
                                              NotchID = g.NotchID,
                                              HireDate = e.HireDate,
                                              StaffLocationID = e.StaffLocationID,
                                              FirstName = c.FirstName,
                                              LastName = c.LastName,
                                              MiddleName = c.MiddleName,
                                              FullName = c.FirstName + (c.MiddleName == null ? "" : " " + c.MiddleName) + " " + c.LastName + " (" + u.Title + ")",
                                              SalutationID = c.SalutationID,
                                              GenderID = c.GenderID,
                                              EthnicityID = c.EthnicityID,
                                              NationalityID = c.NationalityID,
                                              Photograph = e.Photograph
                                          })
                    .AsEnumerable()
                    .OrderBy(m => m.EmployeeNumber)
                    .ThenBy(m => m.FirstName);

            return emp;
        }
        public ActionResult ActiveEmployees_Read([DataSourceRequest] DataSourceRequest request)
        {
            _Employees = GetEmployees();
            return Json(_Employees.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);
        }    

    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
名称空间Mymvc.Controllers
{
公共类控制器:控制器
{
私有只读MyEntities _db=new MyEntities();
私人IEnumerable_员工;
//获取:员工
公共行动结果索引()
{
ViewData[“ModuleName”]=“在职员工”;
ViewData[“deptIDList”]=\u db.lu\u Department.OrderBy(e=>e.Dept);
ViewData[“jobTitleIDList”]=\u db.lu\u JobTitle.OrderBy(e=>e.JobTitle);
ViewData[“genderList”]=\u db.lu\u Gender.OrderBy(e=>e.Gender);
ViewData[“种族列表”]=\u db.lu\u种族.OrderBy(e=>e.族裔);
ViewData[“nationalityList”]=\u db.lu\u national.OrderBy(e=>e.national);
ViewData[“staffLocationList”]=\u db.lu\u StaffLocation.OrderBy(e=>e.StaffLocation);
ViewData[“notchList”]=\u db.lu\u EmploymentGradeNotch.OrderBy(e=>e.Notch);
ViewData[“敬礼列表”]=\u db.lu\u敬礼.OrderBy(e=>e.Title);
返回视图();
}
公共操作结果EmployeeDetails(int employeeID=0)
{
veEmployee SelectedEmployee=null;
ViewData[“deptIDList”]=\u db.lu\u Department.OrderBy(e=>e.Dept);
ViewData[“jobTitleIDList”]=\u db.lu\u JobTitle.OrderBy(e=>e.JobTitle);
ViewData[“genderList”]=\u db.lu\u Gender.OrderBy(e=>e.Gender);
ViewData[“种族列表”]=\u db.lu\u种族.OrderBy(e=>e.族裔);
ViewData[“nationalityList”]=\u db.lu\u national.OrderBy(e=>e.national);
ViewData[“staffLocationList”]=\u db.lu\u StaffLocation.OrderBy(e=>e.StaffLocation);
ViewData[“notchList”]=\u db.lu\u EmploymentGradeNotch.OrderBy(e=>e.Notch);
ViewData[“敬礼列表”]=\u db.lu\u敬礼.OrderBy(e=>e.Title);
尝试
{
如果(employeeID==0)
employeeID=_db.Employees.OrderBy(m=>m.EmployeeNumber).First().employeeID;
如果(_Employees==null)
_Employees=GetEmployees();
SelectedEmployee=\u Employees.First(e=>e.EmployeeID==EmployeeID);
}
捕获(例外e)
{
var msg=e.消息;
}
返回视图(选定的员工);
}
私有IEnumerable GetEmployees()
{
IEnumerable emp=(来自e in _db.Employees
在e.EmployeeID上加入c_db.Contacts等于c.EmployeeID
将g加入e.NotchID上的_db.lu_EmploymentGradeNotch等于g.NotchID
在c上加入u db.lu_称呼。称呼ID等于sal中的u TitleID
来自sal.DefaultIfEmpty()中的u
将s加入e.SubDeptID上的_db.lu_子部门等于s.SubDeptID加入sde
来自sde.DefaultIfEmpty()中的
在e.StaffLocationID上的_db.lu_StaffLocation中加入l等于在cl中加入l.StaffLocationID
来自cl.DefaultIfEmpty()中的cm
将t加入c.EthnicityID等于t.EthnicityID加入eth
来自eth.DefaultIfEmpty()中的et
加入n在c上加入b.lu国籍。NationalityID等于n.NationalityID进入nt
从nt.DefaultIfEmpty()中的nt1开始
其中c.ContactTypeID==1和e.EmploymentStatusID==1
选择新员工
{
EmployeeID=e.EmployeeID,
EmployeeNumber=e.EmployeeNumber,
DeptID=e.DeptID,
SubDeptID=e.SubDeptID,
JobTitleID=e.JobTitleID,
ReportsToID=e.ReportsToID,
NotchID=g.NotchID,
HireDate=e.HireDate,
StaffLocationID=e.StaffLocationID,
FirstName=c.FirstName,
LastName=c.LastName,
MiddleName=c.MiddleName,
FullName=c.FirstName+(c.MiddleName==null?“:“+c.MiddleName)++c.LastName+”(“+u.Title+”),
saltationId=c.saltationId,
GenderID=c.GenderID,
EthnicityID=c.EthnicityID,
NationalityID=c.NationalityID,
照片
})
.可计算的()
.OrderBy(m=>m.EmployeeNumber)
.ThenBy(m=>m.FirstName);
返回emp;
}
公共行动结果已读取([Dat
@using Mymvc.Models
@model Mymvc.Models.vEmployee
@using System.Collections

<div class="col col-xs-6">
        @(Html.Kendo().Grid<vEmployee>()
        .Name("EmployeesList")
        .Columns(columns =>
        {
            columns.Bound(p => p.EmployeeNumber).Width(35);
            columns.Bound(p => p.FirstName).Width(60);
            columns.Bound(p => p.LastName).Width(60);
            columns.ForeignKey(p => p.JobTitleID, (IEnumerable)ViewData["jobTitleIDList"], "JobTitleID", "JobTitle")
                    .Width(60);
            columns.ForeignKey(p => p.DeptID, (IEnumerable)ViewData["deptIDList"], "DeptID", "Dept")
                    .Width(60);
        })
        .Events(e => e.Change("Grid_OnRowSelect"))
        .Events(e => e.DataBound("Grid_OnDataBound"))
        .DataSource(dataSource => dataSource
            .Ajax()
            .PageSize(15)
            .ServerOperation(false)
            .Events(events => events.Error("grid_error")) // Handle the "error" event
            .Model(model =>
            {
                model.Id(m => m.EmployeeID);
                model.Field(m => m.EmployeeID).Editable(false);
            })
            .Read(read => read.Action("ActiveEmployees_Read", "Employee"))
            )
            .Filterable()
            .Pageable()
            .Sortable()
            .Selectable()
        )
    </div>

     @Html.Partial("_EmployeeDetails");

    <script type="text/javascript">  
        Grid_OnDataBound = function (e) {
            var grid = $("#EmployeesList").data("kendoGrid");
            grid.select(e.sender.tbody.find("tr:first"));
        }

        Grid_OnRowSelect = function (e) {
            var data = this.dataItem(this.select());
            var empID = data.id;//IMP
            $.ajax({
                url: '/Employee/EmployeeDetails',
                type: "GET",
                //dataType: "JSON",
                data: { 'employeeID': empID }
            });
        }
    </script>
@model Mymvc.Models.vEmployee
@using System.Collections

<div class="col col-xs-6">
    @using (Html.BeginForm("EmployeeDetails", "Employee"))
    {
        <div class="container">

            <div class="col-md-8 well">
                <div class="form-horizontal">
                    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

                    <div id="updateMsg" class="demo-section k-content"></div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-3" })
                        <div class="col-md-5">
                            @Html.TextBoxFor(model => model.LastName, new { style = "width: 400px" })
                            @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-3" })
                        <div class="col-md-5">
                            @Html.TextBoxFor(model => model.FirstName, new { style = "width: 400px" })
                            @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.MiddleName, htmlAttributes: new { @class = "control-label col-md-3" })
                        <div class="col-md-5">
                            @Html.TextBoxFor(model => model.MiddleName, new { style = "width: 400px" })
                            @Html.ValidationMessageFor(model => model.MiddleName, "", new { @class = "text-danger" })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.SalutationID, htmlAttributes: new { @class = "control-label col-md-3" })
                        <div class="col-md-2">
                            @Html.DropDownListFor(model => model.SalutationID, new SelectList((IEnumerable)ViewData["salutationList"], "TitleID", "Title"))
                            @Html.ValidationMessageFor(model => model.SalutationID, "", new { @class = "text-danger" })
                        </div>

                        @Html.LabelFor(model => model.GenderID, htmlAttributes: new { @class = "control-label col-md-2 col-md-offset-2" })
                        <div class="col-md-1">
                            @Html.DropDownListFor(model => model.GenderID, new SelectList((IEnumerable)ViewData["genderList"], "GenderID", "Gender"))
                            @Html.ValidationMessageFor(model => model.GenderID, "", new { @class = "text-danger" })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.NationalityID, htmlAttributes: new { @class = "control-label col-md-3" })
                        <div class="col-md-1">
                            @Html.DropDownListFor(model => model.NationalityID, new SelectList((IEnumerable)ViewData["nationalityList"], "NationalityID", "Nationality"))
                            @Html.ValidationMessageFor(model => model.NationalityID, "", new { @class = "text-danger" })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.EthnicityID, htmlAttributes: new { @class = "control-label col-md-3" })
                        <div class="col-md-1">
                            @Html.DropDownListFor(model => model.EthnicityID, new SelectList((IEnumerable)ViewData["ethnicityList"], "EthnicityID", "Ethnicity"))
                            @Html.ValidationMessageFor(model => model.EthnicityID, "", new { @class = "text-danger" })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.Photograph, htmlAttributes: new { @class = "control-label col-md-3" })
                        <div class="col-md-5">
                            @Html.TextBoxFor(model => model.Photograph, new { style = "width: 180px" })
                            @Html.ValidationMessageFor(model => model.Photograph, "", new { @class = "text-danger" })
                        </div>
                    </div>
                </div>

                <style>
                    .demo-section {
                        text-align: center;
                        line-height: 4em;
                    }
                </style>
            </div>
        </div>
    }

</div>
@Html.Action("_EmployeeDetails", new { id = yourSelectedRowIdValue })
  public ActionResult _EmployeeDetails (int id)
        {
            // var employeeDetail = find _EmployeeDetail using by id                   
            return View (employeeDetail );
        }