0

インデックスページに部分ビューEnquiryFormがあり、部分ビューからデータを投稿しようとしています。

 [HttpPost]
 public ActionResult EnquiryForm(Booking model)
 {
    if (ModelState.IsValid)
    {
       ........
    }
    return RedirectToAction("Index", "Home");
 }

フォームが空白で投稿された場​​合、検証が機能しない、サーバー側の検証

予約モデル

public class Booking
{
    [Required(ErrorMessage = " - Required" )]
    public string Name { get; set; }

    [Required(ErrorMessage = " - Required")]
    [RegularExpression(".+@.+\\..+", ErrorMessage = "In-Correct Email")]
    public string Email { get; set; }
}

よろしくお願いします

4

1 に答える 1

1

検証が false で、ページを「/Home/Index」にリダイレクトした場合、検証メッセージは表示されません。打撃を参照してください:

[HttpPost]
 public ActionResult EnquiryForm(Booking model)
 {
    if (ModelState.IsValid)
    {
       ........ 
    }
  //  return RedirectToAction("Index", "Home"); 
      return View();
 }

または、次のコードを試すことができます。

あなたのコントローラー

[HttpGet]
public ActionResult EnquiryForm()
{          
     return View();
}

[HttpPost]
public ActionResult EnquiryForm(Booking model)
{
   if (ModelState.IsValid)
    {
        return RedirectToAction("Index", "Home");
    }
   return View();
}

お問い合わせフォームビュー

@using (Html.BeginForm("EnquiryForm", "Home", FormMethod.Post))
{       
   @Html.Partial("_EnquiryFormPartialView", Model)

   <input type="submit" id="bt_submit"/>
 }

あなたの部分的な見方

<div>
    @Html.LabelFor(n => n.Name)
    @Html.TextBoxFor(n => n.Name)
    @Html.ValidationMessageFor(n => n.Name)
</div>

<div>
  @Html.LabelFor(n => n.Email)
  @Html.TextBoxFor(n => n.Email)
  @Html.ValidationMessageFor(n => n.Email)
</div>
于 2013-10-12T19:54:35.583 に答える