私はAsp.net MVCでまったく新しいです
フォームに入力してデータベースに投稿したいのですが、モデル Binder の検証が false です。私のモデルにある私のエラーは表示されません
短くできなかった問題が何であるかがわからないため、申し訳ありません:
ここに私のモデルがあります:
public class Request
{
//pkey
public virtual int Id { get; set; }
//Fkey
public virtual int TourId { get; set; }
[Required]
[MaxLength(150, ErrorMessageResourceType = typeof(ErrorResource), ErrorMessageResourceName = "CheckLenght")]
public virtual string FirstName { get; set; }
[Required]
[MaxLength(150, ErrorMessageResourceType = typeof(ErrorResource), ErrorMessageResourceName = "CheckLenght")]
public virtual string LastName { get; set; }
[Required]
[EmailAddress(ErrorMessageResourceType = typeof(ErrorResource), ErrorMessageResourceName = "Email")]
[MaxLength(150, ErrorMessageResourceType = typeof(ErrorResource), ErrorMessageResourceName = "CheckLenght")]
public virtual string Email { get; set; }
[Required]
public virtual string Phone { get; set; }
[MaxLength(100000000, ErrorMessageResourceType = typeof(ErrorResource), ErrorMessageResourceName = "CheckLenght")]
public virtual string Comment { get; set; }
public virtual bool FrequentTraveler { get; set; }
[Required]
[Range(1, 500000)]
public virtual int TravelersCount { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
public virtual string Date { get; set; }
public virtual bool ContactTimePreference { get; set; }
[MaxLength(150, ErrorMessageResourceType = typeof(ErrorResource), ErrorMessageResourceName = "CheckLenght")]
public virtual string Country { get; set; }
public virtual bool Archived { get; set; }
そして、これは私のフォームです:
@using (Html.BeginForm("Create", "Request"))
{
<div class="form-group">
<input type="hidden" name="TourId" value="4"/>
</div>
<div class="form-group">
@Html.EditorFor(model => model.Request.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Request.FirstName, "", new { @class = "text-danger" , placeholder = "FirstName" })
</div>
<div class="form-group">
@Html.EditorFor(model => model.Request.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Request.LastName, "", new { @class = "text-danger" })
</div>
<div class="form-group">
@Html.EditorFor(model => model.Request.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Request.Email, "", new { @class = "text-danger" })
</div>
<div class="form-group">
@Html.EditorFor(model => model.Request.Phone, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Request.Phone, "", new { @class = "text-danger" })
</div>
<div class="form-group ft">
@Html.EditorFor(model => model.Request.FrequentTraveler)
@Html.ValidationMessageFor(model => model.Request.FrequentTraveler, "", new { @class = "text-danger" })
</div>
<div class="form-group">
<input type="hidden" name="TravelersCount" value="3" />
</div>
<div class="form-group">
<input type="hidden" name="TravelersCount" value="3" />
</div>
}
省略形として null を許可するフォーム グループの一部を省略します。
そして、これはリクエストコントローラーでの私の作成アクションです:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,TourId,FirstName,LastName,Email,Phone,Comment,FrequentTraveler,TravelersCount,Date,ContactTimePreference,Country,Archived")] Request request)
{
if (ModelState.IsValid)
{
db.Requests.Add(request);
db.SaveChanges();
return RedirectToAction("Index");
}
return View();
}
誰かが私のリクエストオブジェクトを有効にするための問題を教えてくれたらありがたいです。たとえば、サーバーで必要なファーストネームにnullを入れたときにエラーをユーザーに送信する方法があれば教えてください。