asp.net-mvc-validation

ASP.net MVC Validation Hook

末鹿安然 提交于 2019-12-03 08:42:54
问题 I have the following view in ASP.net MVC 3: @model Models.CreateProjectViewModel <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script> <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script> @using( Html.BeginForm() ) { @Html.TextBoxFor(m => m.ProjectName) @Html.ValidationMessageFor(m => m.ProjectName) <p> <input type="submit" value="Save" /> </p> } I am using unobtrusive javascript with jQuery and

MVC 5 - can not get globalisation running

人盡茶涼 提交于 2019-12-02 17:52:29
问题 I want to add globalization because the site asks the user for a date. And my german user want to type "31.12.1966" and not "1966-12-31". So I add the nuget-Packages "jQuery.Validation.Globalize" and "jquery-globalize" to the project. Now I am not able to configure my BundleConfig! From my research I know, that I Need globalize.js and some other files. So I try to make a bündle: bundles.Add(new ScriptBundle("~/bundles/jquery").Include( "~/Scripts/jquery-{version}.js")); bundles.Add(new

Validate list of models programmatically in ASP.NET MVC

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 00:13:03
问题 I would like to validate list of models one by one programmatically. I tried TryValidateModel but looks like it aggregates validation errors, so that when I iterate through the list of 10 models, if 5th model is not valid, for models 6-10 TryValidateModel returns false . 回答1: You can use controller methods ValidateModel or TryValidateModel . ValidateModel - throws exception if model is not valid. TryValidateModel - returns bool which indicates if model is valid. From MSDN: When a model is

Validate list of models programmatically in ASP.NET MVC

对着背影说爱祢 提交于 2019-12-01 22:05:29
I would like to validate list of models one by one programmatically. I tried TryValidateModel but looks like it aggregates validation errors, so that when I iterate through the list of 10 models, if 5th model is not valid, for models 6-10 TryValidateModel returns false . You can use controller methods ValidateModel or TryValidateModel . ValidateModel - throws exception if model is not valid. TryValidateModel - returns bool which indicates if model is valid. From MSDN : When a model is being validated, all validators for all properties are run if at least one form input is bound to a model

ASP.NET MVC 2 RC client side validation not working

不羁岁月 提交于 2019-12-01 20:11:32
问题 I can't seem to get any client side validation working on a MVC 2 RC app. My model has the following: public class ExampleModel { [Required(ErrorMessage="Test1 is required")] [DisplayName("Test1")] public string Test1 { get; set; } [Required(ErrorMessage="Test2 is required")] [DisplayName("Test2")] public string Test2 { get; set; } } My view has the following code: <% Html.EnableClientValidation(); %> <%= Html.ValidationSummary(true, "Test was unsuccessful.") %> <% using (Html.BeginForm()) {

ASP.NET MVC 2 RC client side validation not working

為{幸葍}努か 提交于 2019-12-01 18:08:01
I can't seem to get any client side validation working on a MVC 2 RC app. My model has the following: public class ExampleModel { [Required(ErrorMessage="Test1 is required")] [DisplayName("Test1")] public string Test1 { get; set; } [Required(ErrorMessage="Test2 is required")] [DisplayName("Test2")] public string Test2 { get; set; } } My view has the following code: <% Html.EnableClientValidation(); %> <%= Html.ValidationSummary(true, "Test was unsuccessful.") %> <% using (Html.BeginForm()) { %> <div> <div class="editor-label">Test1:</div> <div class="editor-field"> <%= Html.TextBoxFor(m => m

jQuery.Validation.Unobtrusive client side validation only works when scripts are on view page

无人久伴 提交于 2019-11-30 20:05:57
I have an ASP.NET MVC 4 App that uses the jQuery.validation.js plugin and MVC's jQuery.validation.unobtrusive.js . I use data annotations on my view model to validate a textbox's input to be an integer. This (nested) view is loaded within a parent view using... <% Html.RenderPartial("New"); %> One the first inital page load, client side validation works. But any reloading of the nested view with an ajax call, client side validation no longer works. Why is that? Update : (Code example from webdeveloper's solution below) $.validator.unobtrusive.parse($('form')); Example: var saveAndUpdate =

MVC data annotations range validation not working properly

随声附和 提交于 2019-11-30 11:22:18
I have a RangeValidator on a property in my model to only allow Integers that are between 0 and 100. I have a partial view that displays a form to update the property via a jQuery UI dialog. I have looked at the source and can confirm that the data annotation attributes are being generated properly. However, the validatation isn't working properly. It does perform some kind of validation, but it isn't using the range I'm setting. The values 1, 10, and 100 do not produce the error. Any other single or two digit value produces the error. However, if I pad with zeros, all values less than one

Remote ViewModel validation of nested objects not working

房东的猫 提交于 2019-11-30 08:59:56
I have a class user which looks like this: public class User { public int UserId { get; set; } [Required(ErrorMessage = "A username is required.")] [StringLength(20, ErrorMessage = "Your username must be 4-20 characters.", MinimumLength = 4)] [RegularExpression("^[a-zA-Z0-9]*$", ErrorMessage = "Your username can only consist of letters and numbers.")] [Remote("UsernameExists", "RemoteValidation", ErrorMessage = "Username is already taken")] public string Username { get; set; } [Required(ErrorMessage = "A password is required.")] [MinLength(4, ErrorMessage = "Your password must have at least 4

MVC data annotations range validation not working properly

♀尐吖头ヾ 提交于 2019-11-29 16:37:37
问题 I have a RangeValidator on a property in my model to only allow Integers that are between 0 and 100. I have a partial view that displays a form to update the property via a jQuery UI dialog. I have looked at the source and can confirm that the data annotation attributes are being generated properly. However, the validatation isn't working properly. It does perform some kind of validation, but it isn't using the range I'm setting. The values 1, 10, and 100 do not produce the error. Any other