【问题标题】:MVC4 validation is not working even i used required即使我使用了必需的 MVC4 验证也不起作用
【发布时间】:2013-10-27 08:49:16
【问题描述】:

我在 model

中有这个类
public class Tenant :User
    {
        [Required]
        public string PassportNumber { get; set; }
        [Required]
        public string image { get; set; }
        [Required]
        public string passportImage { get; set; }
    }

视图我有这个代码:

@using (Html.BeginForm("RegisterTenant", "Tenant", FormMethod.Post, 
                            new { enctype = "multipart/form-data" })){


    <div class="editor-label">
            @Html.LabelFor(model => model.FirstName)
    </div>
    <div class="editor-field">
            @Html.TextBoxFor(x => x.FirstName, new {placeholder = "Enter Your First Name" })
            @Html.ValidationMessageFor(model => model.FirstName)
    </div>
    <div class="editor-label">
            @Html.LabelFor(model => model.LastName)
    </div>
    <div class="editor-field">
            @Html.TextBoxFor(model => model.LastName, new { placeholder = "Enter Your Last Name"})
            @Html.ValidationMessageFor(model => model.LastName)
    </div>
    <div class="editor-label">
            @Html.LabelFor(model => model.Password)
    </div>
    <div class="editor-field">
            @Html.TextBoxFor(model => model.Password, new { placeholder = "Enter Your Password"})
            @Html.ValidationMessageFor(model => model.Password)
    </div>
    <div class="editor-field">
        <label>Password Again</label>
        <input type="text" placeholder="Enter your password again" name="Password2"/>
        <span class="errorMessage"></span>
    </div>
    <div class="editor-label">
            @Html.LabelFor(model => model.MobileNumber)
    </div>
    <div class="editor-field">
            @Html.TextBoxFor(model => model.MobileNumber, new { placeholder = "Enter Your Mobile Number"})
            @Html.ValidationMessageFor(model => model.MobileNumber)
    </div>
    <div class="editor-label">
            @Html.LabelFor(model => model.PassportNumber)
    </div>
    <div class="editor-field">
            @Html.TextBoxFor(model => model.PassportNumber, new {placeholder = "Enter Your Passport Number"})
            @Html.ValidationMessageFor(model => model.PassportNumber)
    </div>
    <div class="editor-field">
        <label for="file">Upload You Passport:</label> 
        <input type="file" name="file" id="passport" style="width: 100%;" />  
        <span class="errorMessage"></span>
    </div>
    <div class="editor-field">
        <label for="file">Upload You Image:</label> 
        <input type="file" name="file" id="image" style="width: 100%;" />  
        <span class="errorMessage"></span>
    </div>
    <input type="submit" value="Register"  class="submit"/>
}

我的问题

即使我使用了验证消息和required 标签,当我按下提交按钮时,尽管字段为空,它仍然有效。

我做错了什么?

【问题讨论】:

  • 只是为了确定,是在谈论客户端还是服务器端验证?
  • 您是否在视图中包含了 Jquery 验证文件?
  • @AirL 客户端验证
  • @AbbasAmiri 我用互联网应用程序创建了新的 mvc 4,所以我认为它已经安装了
  • @MarcoDinatsoli 不,您不应该手动操作

标签: asp.net-mvc asp.net-mvc-4 html-helper


【解决方案1】:

当您要在视图中使用 JQuery 验证时,您必须在视图中包含所需的 JQuery 验证文件。

@section scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

【讨论】:

  • 我在我的模型中使用了这个[Required(ErrorMessage= "Required")],是的,它可以工作,但如果我输入特殊字符或数字,它也可以在字段为空的情况下工作......,为什么请
  • 不不,伙计,我希望一些文本字段只接受字符,而其他文本字段只接受数字(如手机)。另外,我有两个密码字段,我想在发送到服务器之前确保它们相等,如果你愿意,我可以再做一个问题,你在那里回答。因此,你得到了更多的要求:P
  • 当然,你认为你需要掩码工具,如果你再提出一个问题,我会教你一个最好的。
  • 请检查我的问题,我
猜你喜欢
  • 2011-01-05
  • 2013-06-08
  • 1970-01-01
  • 2019-06-17
  • 1970-01-01
  • 1970-01-01
  • 2018-10-29
  • 2023-04-06
  • 2020-04-17
相关资源
最近更新 更多