2017-09-06 2 views
0

Linq에 대한 문제가 있습니다. 나는 필요한 모든 필드 (Holiday)와 다른 모델 (Profile)을 가진 폼을 가지며, 다시 모든 필드가 필요합니다.Linq은 null 값을 반환합니다.

일부 휴일 속성 양식이 데이터베이스의 일부 프로필 속성으로 자동 채워지려고 휴일 요청 양식을 차단하려고합니다.

[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; }  

} 

그리고 이것은 내 프로필 모델 :

<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에서

이 작업을 수행하려면 다음

[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; } 
} 

내가 뭘하려고 오전이 같은 양식을하는 것입니다 :

[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); 
    } 

어떻게 든 값은 테이블 Profile에 있기 때문에 값을 반환해야하더라도 모든 vars는 null을 반환합니다.

내가 뭘 잘못하고 있는지 말해 주실 수 있습니까? 나는 오류가

+3

디버깅을 시도 했습니까? 오류가 있습니까? 작은 샘플로 특정 문제를 제공하는 것이 더 좋을 것이며 모든 코드를 다루는 것은 아닙니다. – Airborne

+0

안녕하세요, 디버깅을 시도했습니다. 나는 컨트롤러의 각 var에 대해서도 if 문에 breakpoint를 둔다. 그것은 내게 오류를 준다 : ModelState.IsValid = false –

+1

문제를 재현하는 데 필요한 최소한의 코드를 제공 할 수 있습니까? 제공 한 코드의 벽은 읽기가 어렵습니다. 그리고 그 오류 메시지는 질문으로 편집되어야합니다. –

답변

-2

Sysem.Web.Mvc.ModelError, ErrorMessage: "The FirstName field is required" and its value is set to null.에서 오는이 당신이 FirstOrDefault()를 시도해야

+0

OP가이 라인을 추가해야한다고 제안하는 곳은 어디입니까? 이것이 어떻게 문제를 해결할 것입니까? 당신은 당신의 대답에 더 많은 세부 사항과 설명을 추가 할 수 있습니까? –

+0

FirstOrDefault()와 Single()을 시도했습니다. –

+0

결과가 null 인 경우 Single()이 예외를 throw합니다. –

0
당신이 행동으로 전달하는 모델 이름, 성 등이 있지만, 빈 값을 가지고 있기 때문에 ModelState.IsValid false를 반환

필요한 것으로 표시되어 있습니다.

양식에 포함 할 속성 만 포함하는 별도의보기 모델을 만든 다음 해당 값을 게시 작업의 HolidayViewModel에 매핑합니다.

관련 문제