2013-10-12 37 views
0

我有局部視圖EnquiryForm我的索引頁面,我正在嘗試從部分視圖發佈數據。MVC 4剃刀。部分查看錶單驗證

[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("[email protected]+\\..+", ErrorMessage = "In-Correct Email")] 
    public string Email { get; set; } 
} 

問候 感謝

+0

顯示視圖頁面和局部視圖。如果您使用剃鬚刀視圖,我建議您嘗試模板。 – Miller

+0

是的我正在使用剃刀,關於如何使用模板的任何鏈接 – Moksha

+0

http://coding-in.net/asp-net-mvc-3-how-to-use-editortemplates/這可能是幫助 – Miller

回答

0

如果確認是錯誤的,你將頁面重定向到「/ 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(); 
} 

你EnquiryForm查看

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

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

Your Parital view

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