I have a required string for my model and I can submit the form and still leave the textbox blank and the model state is being returned as valid so I have no idea what I did wrong. This is the only control on my page that is doing this. Here is my code:
Model
[Required(ErrorMessage = "Requirements are Required.")]
[DataType(DataType.MultilineText, ErrorMessage = "Requirements is Required.")]
[StringLength(200, ErrorMessage = "200 Characters is the maximum allowed for requirements.")]
public string Requirements { get; set; }
View
<div class="form-group">
#Html.LabelFor(model => model.Requirements, "Requirements:", htmlAttributes: new { #class = "col-sm-4 control-label" })
<div class="col-sm-8">
#Html.TextAreaFor(model => model.Requirements, htmlAttributes: new { #rows = "10", #cols = "50", #class = "form-control" })
#Html.ValidationMessageFor(model => model.Requirements)
</div>
Edit:
As #Stephen Muecke noted, I cant reproduce your case neither, it should be enough Required attribute.
My answer below somehow Workaround as said by #MajkeloDev.
You should put MinimumLength = 10 for your field. number 200 indicates maximum length for your string
[StringLength(200, MinimumLength = 10, ErrorMessage = "200 Characters is the maximum allowed for requirements.")]
Note: MinimumLength = 10 is being some number for a meaningful input. For non empty string it is enough MinimumLength = 1
Related
This is my model's date property:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy.mm.dd}")]
[Required(ErrorMessage = "You need to fill this")]
[DataType(DataType.DateTime, ErrorMessage = "Type proper date")]
[Display(Name = "Weight Date")]
public DateTime? WeightDate { get; set; }
This is how I render bootstrap's datepicker
#Html.EditorFor(x => x.WeightDate, new { htmlAttributes = new { #id="WeightDate" #class = "form-control datepicker", #required = "required" }})
#Html.ValidationMessageFor(m => m.WeightDate, "", new { #class = "text-danger" })
Validation seems to working (I can see "You need to fill this" when entering empty datepicker value). The problem is that I can see other messages, which I would don't want to see (or at least change their messages):
When I enter invalid data, I am getting error (from client side):
The field Weight date must be a date.
When I submit invalid data, I am getting error (from server side):
The value '202441.02.03' is not valid for Weight date.
How can I change those messages?
When I add any regular expression pattern in MVC for textarea it's not working. When I click on submit button the wrong value is added to database
My View Code:
#Html.TextAreaFor(model => model.MyEntity.Item, new { #required = "required", #class = "form-control english_only", #placeholder = "Item", #maxlength = "120", #Pattern = #"[^A-Za-z0-9]" })
My Model Code:
[StringLength(150)]
public string Item{ get; set; }
This is just for any other who have this problem...
Based on Developers comments:
HTML5 <textarea> element does not support pattern attribute
so the solution is by MVC built in validation (with jquery.validate.js) and jquery.validate.unobtrusive.js)
So this problem solve by change my model to be:
[StringLength(150)]
[RegularExpression("^[0-9]*$", ErrorMessage = "UPRN must be numeric")]
public string Item{ get; set; }
and also include the jquery.validate.js in my view or partial view
Ore simply change my html helper to be text box instead of text Area as following:
#Html.EditorFor(model => model.Item, new { htmlAttributes = new { #class = "form-control english_only", #placeholder = "Only english text", #required = "required", #Pattern = #"^[a-zA-Z0-9$#$!%*?&#^-_. +]+$", #title = "please insert english text only", #autocomplete= "off", #maxlength = "120" } })
Whenever I am trying to insert records it gives me error message for password field as '
The value 'SomePassword' is not valid for password
Model
public byte[] Password { get; set; }
View
<label class="input">
<i class="icon-append fa fa-tag"></i>
#Html.PasswordFor(model => model.Class.Password, new { #class = "form-control", #id = "txtPassword" })
<span asp-validation-for="Class.Password" class="text-danger"></span>
</label>
When checked in controller the ModelState is invalid and error message is coming why is it so ?. I have tried with DataType.Password on Password field, but still no success
The Password column has datatype 'Varbinary' in sql server.
Any help on this appreciated !
A password is never entered by the user as a byte arrray, it is converted into one before hashing.
The mvc model binder has no built in capability to convert any input to a byte array, and even though you could write a custom model binder I don't see why you would want to, as a plain string is much easier to type.
Even though the SQL type may be varbinary, you do not want the user to enter this representation in your model.
You should set the Class.Password property to be a string, and then in your server side code, you should be hashing the password.
Encoding.UTF8.GetBytes(password); will convert the string password into a byte[] but it on it's own it is not sufficient for secure password storage.
I strongly recommend you take a look at https://www.asp.net/identity if you have the option to upgrade your password requriements.
why dont you start with the Password model of the Default Account, and modify it to fit your actual need? or you have a very unusal password requirement which need byte[]? otherwise you should just convert it when you actually needed to...
Model
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
View
<div class="form-group">
#Html.LabelFor(m => m.Password, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.PasswordFor(m => m.Password, new { #class = "form-control" })
</div>
</div>
What you wrote would only work if class Model has a property called Class, and the property Class has a property Password.
Try using this instead:
#Html.PasswordFor(model => model.Password, ...
Also, the Password property in your ViewModel needs to be a string. Both the Web and MVC cannot use byte[] for text input controls.
You can store a byte[] - hashed and encrypted, the best approach! - but it first comes in as a string.
This question already has answers here:
MVC4 input field placeholder
(10 answers)
Closed 7 years ago.
In my ViewModel for my partial view I have Email defined like this:
[Display(Name = "Email")]
[EmailAddress]
public string Email { get; set; }
In the view I have this:
#Html.TextBoxFor(m => m.Email, new {#class = "form-control"})
#Html.ValidationMessageFor(m => m.Email, "", new { #class = "text-danger" })
I would like something like 'example#company.com' to appear in the textbox as a placeholder or example. But right now it is empty.
You can set a placeholder attribute (HTML5) to the form element. Browsers which supports this feature will add a placeholder text to your textbox.
#Html.TextBoxFor(s=>s.Email, new { #class="form-control", placeholder="sample#ss.com"})
Use this to add a placeholder attribute to your HTML:
#Html.TextBoxFor(m => m.Email, new {#class = "form-control", placeholder = "example#company.com"})
I am trying to get #String.Format("{0:0.00}",Model.CurrentBalance) into this #Html.TextBoxFor(model => model.CurrentBalance, new { #class = "required numeric", id = "CurrentBalance" })
I just want the currency to show up as .00 inside of my textbox but am having no luck. Any ideas on how I do this?
string.format("{0:c}", Model.CurrentBalance) should give you currency formatting.
OR
#Html.TextBoxFor(model => model.CurrentBalance, new { #class = "required numeric", id = "CurrentBalance", Value=String.Format("{0:C}",Model.CurrentBalance) })
#Html.TextBoxFor(model => model.CurrentBalance, "{0:c}", new { #class = "required numeric", id = "CurrentBalance" })
This lets you set the format and add any extra HTML attributes.
While Dan-o's solution worked, I found an issue with it regarding the use of form-based TempData (see ImportModelStateFromTempData and ExportModelStateToTempData). The solution that worked for me was David Spence's on a related thread.
Specifically:
[DisplayFormat(DataFormatString = "{0:C0}", ApplyFormatInEditMode = true)]
public decimal? Price { get; set; }
Now if you use EditorFor in your view the format specified in the annotation should be applied and your value should be comma separated:
<%= Html.EditorFor(model => model.Price) %>