ValidationMessageFor不显示错误消息MVC

时间:2018-05-01 09:40:37

标签: c# asp.net-mvc model

尝试将正确的手机号码输入文本框。我使用此Tutorial自定义了我的模型。但是我使用bootstrap,在那个链接中Css不同。我的问题甚至是我设置了validationError消息,它没有在我的视图中显示。我怎么能这样做? 这是我的模特

  public partial class sign_up
{
    public int id { get; set; }
    public string name { get; set; }
    public string surname { get; set; }
    public string password { get; set; }
    public string e_mail { get; set; }
    public string addrres { get; set; }


    [Display(Name = "Mobile Phone :")]
    [Required(ErrorMessage = "Mobile Number is required.")]
    [RegularExpression(@"^([0-9]{10})$", ErrorMessage = "Invalid Mobile Number.")]
    public string phone { get; set; }

}

MyView代码

 <div class=" form-group">
        <div class="col-md-6">

            @Html.LabelFor(m => m.phone)
            @Html.EditorFor(m => m.phone, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(m => m.phone,"", new { htmlAttributes= new { @style = "color:red" } })


        </div>
    </div>

并且我尝试了Site.css更改,但它没有工作,我没有发现我的错误,它在哪里?

完整视图

@model Ciftlikten.Models.sign_up





@using (Html.BeginForm("Sign_up", "SignUp", FormMethod.Post))
{
    <div class="container">
    <div class="form-group">
        <div class="col-md-6">
            <label>Name: </label>
            @Html.EditorFor(m => m.name, new { htmlAttributes = new { @class = "form-control" } })

        </div>
    </div>

    <div class="form-group">
        <div class="col-md-6">
            <label>Surname :</label>
            @Html.EditorFor(m => m.surname, new { htmlAttributes = new { @class = "form-control" } })

        </div>
    </div>

    <div class=" form-group">
        <div class="col-md-6">
            <label>E-Mail :</label>
            @Html.EditorFor(m => m.e_mail, new { htmlAttributes = new { @class = "form-control" } })

        </div>
    </div>

    <div class=" form-group">
        <div class="col-md-6">
            <label>Password :</label>
            @Html.PasswordFor(m => m.password, new { htmlAttributes = new { @class = "form-control" } })

        </div>
    </div>

    <div class=" form-group">
        <div class="col-md-6">
            <label>Addrres :</label>
            @Html.EditorFor(m => m.addrres, new { htmlAttributes = new { @class = "form-control" } })

        </div>
    </div>


    <div class=" form-group">
        <div class="col-md-6">

            @Html.LabelFor(m => m.phone)
            @Html.EditorFor(m => m.phone, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(m => m.phone,"", new { htmlAttributes= new { @style = "color:red" } })


        </div>
    </div>



<div class="container col-lg-12">
    <div class="container">
        <div class="col-lg-12">
            <input class="btn  btn-block btn-success" type="submit" />
        </div>
    </div>
</div>


}

1 个答案:

答案 0 :(得分:-1)

  • 您提供的链接主要涉及电话号码REGEX和POST方法未正确实施。

  • 同样在你的Razor View中,Jquery Validate脚本似乎缺失了。只有添加了jquery validate脚本,验证才会在客户端进行。否则,它总是在服务器端检查。

  • 在Razor View的末尾添加脚本部分。我假设在_Layout.cshtml中添加了jquery,并且在正文标记结束之前有一个@RenderSection("scripts", required: false)

    @*Add this at end of razor view*@
     @section Scripts {
       @Scripts.Render("~/bundles/jqueryval")
    }
    
    [HttpPost, ValidateAntiForgeryToken]
    public ActionResult Sign_Up(Sign_up sign_upmodel)
    {
        if (ModelState.IsValid)
        {
            // Your logic here. Save to DB etc..
            return RedirectToAction("Index"); // return to some page.
        }
        // Model Validation Failed so return the same Get View.
        return View(sign_upmodel);
    }