Linq返回null值

时间:2017-09-06 14:19:42

标签: c# sql-server linq

我遇到关于Linq的问题。我有一个表格,其中包含所有必填字段(假日)和另一个模型(简介),同样需要所有字段。

我正在尝试从假期请求表单中删除一些Holiday属性,这些属性将自动填充数据库中的一些Profile属性。

[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,即使它们应该返回一个值,因为表中的值存在于表中。

你可以告诉我我做错了什么吗? 我的错误来自Sysem.Web.Mvc.ModelError, ErrorMessage: "The FirstName field is required" and its value is set to null.

2 个答案:

答案 0 :(得分:0)

ModelState.IsValid返回false,因为您传递给操作的模型具有FirstName,LastName等的空白值,但它们被标记为必需。

创建一个单独的视图模型,该模型仅包含要包含在表单中的属性,然后在post操作中将这些值映射到HolidayViewModel。

答案 1 :(得分:-2)

你试过FirstOrDefault()

吗?