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을 반환합니다.
내가 뭘 잘못하고 있는지 말해 주실 수 있습니까? 나는 오류가
디버깅을 시도 했습니까? 오류가 있습니까? 작은 샘플로 특정 문제를 제공하는 것이 더 좋을 것이며 모든 코드를 다루는 것은 아닙니다. – Airborne
안녕하세요, 디버깅을 시도했습니다. 나는 컨트롤러의 각 var에 대해서도 if 문에 breakpoint를 둔다. 그것은 내게 오류를 준다 : ModelState.IsValid = false –
문제를 재현하는 데 필요한 최소한의 코드를 제공 할 수 있습니까? 제공 한 코드의 벽은 읽기가 어렵습니다. 그리고 그 오류 메시지는 질문으로 편집되어야합니다. –