将数据从控制器传递到视图

时间:2017-03-12 00:12:46

标签: c# asp.net-mvc

我是MVC的初学者,我已经构建了一个简单的应用程序,在此我有这个模型:

using System;
using System.Collections.Generic;

namespace WebAppEmployee2.Models
{ 
    public partial class tblEmployee
    {
        public int EmployeeId { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public string City { get; set; }
        public Nullable<int> DepartmentId { get; set; }

        public virtual tblDepartment tblDepartment { get; set; }
    }
}

和部门模型是:

using System;
using System.Collections.Generic;

namespace WebAppEmployee2.Models
{ 
   public partial class tblDepartment
   {
        [System.Diagnostics.CodeAnalysis.SuppressMessage  ("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public tblDepartment()
        {
            this.tblEmployees = new HashSet<tblEmployee>();
        }

        public int Id { get; set; }
        public string Name { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<tblEmployee> tblEmployees { get; set; }
    }
}
HomeController中的

我已经采取行动来创建新用户,这项工作正常。进入另一个行动我会选择员工的所有实体,我做了这个:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var context = new EmployeesEntities();
        EmployeesViewModel evm = new EmployeesViewModel();
        evm.selectall = context.tblEmployees.SqlQuery("SELECT * FROM tblEmployee").ToList();

        return View(evm);
        //return View();
    }
// some other stuff here

}

我会传递一份员工列表进行查看,然后创建一个以下的viewmodel:

public class EmployeesViewModel:tblEmployee
{
    public List<tblEmployee> selectall;
    public EmployeesViewModel()
    {

    }

}

最后在我看来:

@model IEnumerable<WebAppEmployee2.ViewModels.EmployeesViewModel>

@{
   ViewBag.Title = "List of all Employees";
}

<h2>Index</h2>

<table class="table">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.Name)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Gender)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.City)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.tblDepartment.Name)
    </th>
    <th></th>
</tr>

@foreach (var item in Model) 
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Gender)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.City)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.tblDepartment.Name)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.EmployeeId }) |
            @Html.ActionLink("Details", "Details", new { id=item.EmployeeId }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.EmployeeId })
        </td>
    </tr>
}

</table>

但我在foreach中有错误,我的模型为空。

我已将所有这些用于将实体列表传递给我的观点,这是一种很好的做法吗?

为什么模型在视图中为空?

0 个答案:

没有答案