私は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; }
}
そして、これが私のプロフィールモデルである:これを行うには
<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に存在するので、値を返す必要があっても、すべての変数はnullを返しています。
私が間違っていることを教えてください。 私はSysem.Web.Mvc.ModelError, ErrorMessage: "The FirstName field is required" and its value is set to null.
デバッグを試しましたか?エラーが発生していますか?小さなサンプルで特定の問題を提供し、作業しているすべてのコードではないことをお勧めします。 – Airborne
こんにちは、私はデバッグを試みました。私はコントローラの各varについてもifステートメントantにブレークポイントを設定します。それは私にエラーを与える:ModelState.IsValid = false –
問題を再現するために必要な最小限のコードを提供できますか?あなたが提供したコードの壁は読みにくいです。そのエラーメッセージを質問に編集する必要があります。 –