C#MVC无法从视图到控制器检索数据

时间:2018-09-15 15:43:46

标签: c# asp.net asp.net-mvc entity-framework razor

我是ASP.Net的新手,我相信这是一个非常基本的问题。我有一个员工粗鲁的看法。在创建视图中,我创建了viewModel以传递不同的两个模型。一个模型显示员工,另一个模型连接到db并检索我视图中的所有部门。但是我无法将数据检索到我的员工创建控制器区域。将此错误发送给Abc.Models.MyViewModel.Employee.get返回null。在Debug.Print(employee.Employee.Name)行上。

这里是我的模型;

Employee.cs

 public class Employee
{
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public string Surname { get; set; }
    [Required]
    public string phoneNumber { get; set; }
    public string Detail { get; set; }

    //Bu kısımda veri tabanı ilişkisi 1 to many olacak
    public int DepartmentId { get; set; }
    public Department Department { get; set; }
}

Department.cs

public class Department
{
    public int Id { get; set; }
    public string depName { get; set; }

    public List<Employee> Employees { get; set; }
}

MyViewModel.cs

    public class MyViewModel
{
    public Employee Employee { get; set; }
    public IEnumerable<Department> departments { get; set; }
}

我使用MyViewModel在create.cshtml区域中添加了dropboxlist,但无法在控制器上检索所有数据。

这是我的EmployeeController.cs;

// GET: Employee/Create
    [HttpGet]
    public ActionResult Create()
    {

        MyViewModel viewModel = new MyViewModel();
        Employee emp = new Employee();
        viewModel.Employee = emp;
        viewModel.departments = db.Departments;

        return View(viewModel);
    }

    // POST: Employee/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(/*[Bind(Include = "Id,Name,Surname,phoneNumber,Department,Detail")]*/ MyViewModel employee)
    {
        //string emName = employee.Employee.Name;
        Debug.Print(employee.Employee.Name);
        if (ModelState.IsValid)
        {
            db.Employees.Add(employee.Employee);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(employee);
    }

Create.cshtml“此html代码位于@using(Html.BeginForm()“);

@model TelefonRehberi.Models.MyViewModel

<div class="form-horizontal">
    <h4>Employee</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.Employee.Name, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Employee.Name, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Employee.Name, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Employee.Surname, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Employee.Surname, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Employee.Surname, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Employee.phoneNumber, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Employee.phoneNumber, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Employee.phoneNumber, "", new { @class = "text-danger" })
        </div>
    </div>
    <!--Dropdown List Olacak-->
    <div class="form-group">
        @Html.LabelFor(model => model.Employee.Department, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <!--Burada zorlandım umarım doğru kullanım olmuştur.-->
            @Html.DropDownListFor(m => m.Employee.Department, new SelectList(Model.departments.Select(i => i.depName)), " - Select or Add -", new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Employee.Department, "", new { @class = "text-danger" })
        </div>

    </div>
    <!--Dropdown List Sonu Ayarlamalar Yapılacak.-->
    <div class="form-group">
        @Html.LabelFor(model => model.Employee.Detail, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Employee.Detail, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Employee.Detail, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>

2 个答案:

答案 0 :(得分:0)

添加表格并尝试做

@using (Html.BeginForm("ActionName","ControllerName", FormMethod.Post))
{
    <div class="form-horizontal">
        <!--add your other code here -->
        <!--add your other code here -->
       <div class="form-group">
          <div class="col-md-offset-2 col-md-10">
              <input type="submit" value="Create" class="btn btn-default" />
          </div>
       </div>
    </div>
}
要从网站访问者那里收集一些数据,需要

HTML表单。例如,在用户注册期间,您希望收集诸如姓名,电子邮件地址,信用卡等信息。

表单将从站点访问者那里获取输入,然后将其发布到后端应用程序(例如CGI,ASP Script或PHP脚本等)。后端应用程序将根据定义的内容对传递的数据执行所需的处理应用程序内部的业务逻辑。

有各种可用的表单元素,例如文本字段,textarea字段,下拉菜单,单选按钮,复选框等。

答案 1 :(得分:0)

好的,你真的很近。在创建方法中,您需要创建 Employee 雇员,而不是 MyViewModel 雇员。因此,在控制器中只需放置此Create方法:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Employee employee)
{
    Debug.Print(employee.Name);
    if (ModelState.IsValid)
    {
        //db.Employees.Add(employee);
        //db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(employee);
}

然后,绑定将可以正常工作,您将从表格中获取员工。 以防万一,下面是创建视图中表单的代码。

<h2>Create</h2>

@using (Html.BeginForm("Create", "Employee", FormMethod.Post))
{
<div class="form-horizontal">
    <h4>Employee</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.Employee.Name, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Employee.Name, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Employee.Name, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Employee.Surname, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Employee.Surname, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Employee.Surname, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Employee.phoneNumber, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Employee.phoneNumber, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Employee.phoneNumber, "", new { @class = "text-danger" })
        </div>
    </div>
    <!--Dropdown List Olacak-->
    <div class="form-group">
        @Html.LabelFor(model => model.Employee.Department, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <!--Burada zorlandım umarım doğru kullanım olmuştur.-->
            @Html.DropDownListFor(m => m.Employee.Department, new SelectList(Model.departments.Select(i => i.depName)), " - Select or Add -", new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Employee.Department, "", new { @class = "text-danger" })
        </div>

    </div>
    <!--Dropdown List Sonu Ayarlamalar Yapılacak.-->
    <div class="form-group">
        @Html.LabelFor(model => model.Employee.Detail, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Employee.Detail, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Employee.Detail, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
}