Asp.net mvc 数据未从MVC视图正确返回控制器
这是我的模型Asp.net mvc 数据未从MVC视图正确返回控制器,asp.net-mvc,Asp.net Mvc,这是我的模型 using System; using System.ComponentModel.DataAnnotations; namespace thinkBigHR.Models { public class Shift { [Key] public int Id { get; set; } public DateTime Date { get; set; } public Employee Employee { get; set; } } } 这是我
using System;
using System.ComponentModel.DataAnnotations;
namespace thinkBigHR.Models
{
public class Shift
{
[Key]
public int Id { get; set; }
public DateTime Date { get; set; }
public Employee Employee { get; set; }
}
}
这是我的控制器
// POST: Shifts/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Date, Employee")] Shift shift)
{
ClaimsIdentity user = User.Identity as ClaimsIdentity;
if (user.HasClaim("Administrator", "true"))
{
if (ModelState.IsValid)
{
db.Shifts.Add(shift);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(shift);
}
return new HttpStatusCodeResult(HttpStatusCode.Unauthorized);
}
这是我的观点
@using thinkBigHR.Models;
@model thinkBigHR.Models.Shift
@{
ApplicationDbContext db = new ApplicationDbContext();
ViewBag.Title = "Create";
var employees = db.Employees.ToList();
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Shift</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="datetime-local" class="form-control" id="Date" name="Date" />
@Html.ValidationMessageFor(model => model.Date, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2">Assigned Employee:</label>
<select name="Employee" class="form-control">
@foreach (var item in employees)
{
<option value="@item.Id" id="Employee">@item.LegalName (@item.JobTitle)</option>
}
</select>
</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>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
@使用thinkBigHR.Models;
@模型thinkBigHR.Models.Shift
@{
ApplicationDbContext db=新的ApplicationDbContext();
ViewBag.Title=“创建”;
var employees=db.employees.ToList();
}
创造
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
移位
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.Date,htmlAttributes:new{@class=“controllabel col-md-2”})
@Html.ValidationMessageFor(model=>model.Date,“,new{@class=“text danger”})
指定员工:
@foreach(员工中的var项目)
{
@item.LegalName(@item.JobTitle)
}
}
@ActionLink(“返回列表”、“索引”)
@节脚本{
@Scripts.Render(“~/bundles/jqueryval”)
}
因此,它正确地呈现了视图,但我在数据返回控制器后设置了一个断点,它显示只有班次的日期被传回,但没有所选员工的任何详细信息(返回为null).这是因为ASP.NET MVC默认模型绑定器无法确定在班次的员工成员中存储什么 Employee是一个类,您需要创建名为的HTML控件,就像它的成员一样:
<input name="Employee.LegalName" />
<input name="Employee.JobTitle" />
现在,在发布表单后,ASP.NET MVC模型绑定器将知道哪个输入字段属于班次中的员工类的哪个成员
关于默认模型绑定器的详细说明。您可以将
元素绑定到复杂对象,这就是公共雇员{get;set;}
的含义。你需要在你的模型中有一个属性(比如说)intemployee
,并绑定到它。但是,为什么您不使用html助手@html.TextBoxFor()
和@html.DropDownListFor()
将employee
用作名称,我认为它是一个类或枚举或某个对象。因此,为了传递数据,您需要指定哪个字段应该包含像employee[0].Id这样的值,或者创建一些其他属性来传递DDL值