如果输入了无效数据,则RegularExpression Validator不会显示错误消息

时间:2016-01-27 09:41:38

标签: c# entity-framework validation asp.net-mvc-4

我使用以下代码验证.cshtml文件中的文本框。但是当我在文本框中输入无效数据时,RegularExpression Validator不会显示错误消息。

Index.cshtml:

@model RockwellEntityDemo.Areas.Report.Models.StagewiseDCReport

<table width="100%">
            <tr>
                <td>
                    @using (Ajax.BeginForm("DCNoSONoFilter", "StagewiseDelivery", new System.Web.Mvc.Ajax.AjaxOptions
                           {
                               InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace,
                               HttpMethod = "POST",
                               UpdateTargetId = "stagewisedeliverylist"
                           }))
                    {                          
                        <table>     
                            <tr>
                                <th>DC No: </th>                                    
                                <th>   
                                    @Html.TextBoxFor(model => model.DC_NO, new { id="DC_NO" })                                                                             @Html.TextBoxFor(model => model.DC_NO, new { id="DC_NO" })
                                    <font color="red">@Html.ValidationMessageFor(model => model.DC_NO)</font>  
                                </th>                                    
                            </tr>
                            <tr>
                                <th>
                                    <input type="submit" value="View" id="BtnSubmit" />
                                </th>
                            </tr>
                        </table>                            
                    }
                </td>
            </tr>
</table>

在Model Class中,我已将DC_NO声明为字符串:

public class StagewiseDCReport 
{
    [DisplayName("DC No")]
    [RegularExpression(@"^[a-zA-Z0-9]{1,20}$", ErrorMessage = "Alphanumeric Only")] 
    public string DC_NO { get; set; }
}

控制器:

public class StagewiseDeliveryController : MasterController
{
    [HttpPost]
    public ActionResult DCNoSONoFilter(string DC_NO)
    {      
        IEnumerable<StagewiseDCReport> dcms = null;
        //StagewiseDCReport dcms= new StagewiseDCReport();
        try
        {               
            dcms = from t in db.DC_MASTER
                    join t0 in db.CUSTOMER_MASTER on t.CUSTOMER_MASTER.CUS_CODE equals t0.CUS_CODE
                    where
                        1 == 1 && (DC_NO == "" || t.DC_NO == DC_NO)                                                      
                    select new StagewiseDCReport
                    {
                        DC_NO = t.DC_NO,
                        ...                      
                    };                    

            return PartialView("StagewiseDeliveryList",dcms);
        }
        catch (Exception ex)
        {
            TempData["Error"] = "Some error occured while loading the page contact help desk";
            ExceptionUtility.LogException(ex, "StagewiseDelivery-DCNoSONoFilter-POST");
            return PartialView("StagewiseDeliveryList", dcms);
        }

    }
}

0 个答案:

没有答案