我遇到一个关于Linq
的问题。我有一个需要所有字段(假日)和另一个模型(个人资料)的表单,同样需要所有字段。Linq返回空值
我试图从假期请求表格中删除某些假期属性,该属性将自动填充数据库中的一些配置文件属性。
[Table("AspNetHoliday1")]
public class HolidayViewModel
{
[Required]
[Key]
public int Id { get; set; }
[Required]
[MaxLength(50)]
public string FirstName { get; set; }
[Required]
[MaxLength(50)]
public string LastName { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
[MaxLength(50)]
public string TeamLeaderName { get; set; }
[Required]
public DateTime StartDate { get; set; }
[Required]
public DateTime EndDate { get; set; }
[Required]
[Range(1, 21)]
public int DaysOff { get; set; }
[Required]
[EmailAddress]
public string TLEmail { get; set; }
[Required]
public bool Flag { get; set; }
}
这是我的个人资料型号:
[Table("AspNetProfile1")]
public class ProfileViewModel
{
[Required]
public int Id { get; set; }
[Required]
[MaxLength(50)]
public string UserName { get; set; }
[Required]
[MaxLength(50)]
public string FirstName { get; set; }
[Required]
[MaxLength(50)]
public string LastName { get; set; }
[EmailAddress]
public string Email { get; set; }
[Required]
public int Mark { get; set; }
[Required]
[StringLength(13)]
public string CNP { get; set; }
[Required]
[MaxLength(50)]
public string Location { get; set; }
[Required]
[MaxLength(50)]
public string Team { get; set; }
[Required]
[EmailAddress]
public string TeamLeaderEmail { get; set; }
}
我所试图做的是有一个这样的形式:
<div class="form-group">
@Html.LabelFor(model => model.TeamLeaderName, htmlAttributes: new {
@class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TeamLeaderName, new {
htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.TeamLeaderName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.StartDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.StartDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.StartDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EndDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EndDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.EndDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DaysOff, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DaysOff, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DaysOff, "", new { @class = "text-danger" })
</div>
</div>
而且在Holiday Controller
做到这一点:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,FirstName,LastName,Email,TeamLeaderName,StartDate,EndDate,DaysOff,TLEmail,Flag")] HolidayViewModel holidayViewModel)
{
if (ModelState.IsValid)
{
var firstName = (from b in db.ProfileViewModel
where b.UserName.Equals(User.Identity.Name)
select b.FirstName).Single();
holidayViewModel.FirstName = firstName;
var lastName = (from b in db.ProfileViewModel
where b.UserName.Equals(User.Identity.Name)
select b.LastName).Single();
holidayViewModel.LastName = lastName;
var email = (from b in db.ProfileViewModel
where b.UserName.Equals(User.Identity.Name)
select b.Email).Single();
holidayViewModel.Email = email;
var tlEmail = (from b in db.ProfileViewModel
where b.UserName.Equals(User.Identity.Name)
select b.TeamLeaderEmail).Single();
holidayViewModel.TLEmail = tlEmail;
holidayViewModel.Flag = false;
holidayViewModel.TeamLeaderName = Request.Form["TeamLeaderName"];
holidayViewModel.StartDate = Convert.ToDateTime(Request.Form["StartDate"]);
holidayViewModel.EndDate = Convert.ToDateTime(Request.Form["EndDate"]);
holidayViewModel.DaysOff = Convert.ToInt32(Request.Form["DaysOff"]);
db.AspNetHolidays.Add(holidayViewModel);
db.SaveChanges();
return RedirectToAction("SuccessHoliday", "Success");
}
return View(holidayViewModel);
}
不知何故,所有的变量都返回null,即使它们应该返回一个值,因为这些值存在于表格Profile中。
你能告诉我我做错了什么吗? 我从Sysem.Web.Mvc.ModelError, ErrorMessage: "The FirstName field is required" and its value is set to null.
你试过调试吗?你有错误吗?用一个小样本来提供具体的问题会更好,而不是你正在使用的所有代码。 – Airborne
你好,我试过调试。我在if语句ant中也为控制器中的每个var放置了一个断点。它给了我错误:ModelState.IsValid = false –
你能提供重现问题所需的最小代码吗?您提供的代码墙很难通读。那个错误信息应该被编辑到问题中。 –