Validation messages not showing while using same named attributes in the Model? - asp.net-mvc

I have two models i.e. Login and Register:
Login Model
public class LoginModel
{
[Required(ErrorMessage = "Email is required")]
[RegularExpression("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*#[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2,4})$", ErrorMessage = "Not a valid email")]
[DataType(DataType.EmailAddress)]
[DisplayName("Email")]
[StringLength(150, ErrorMessage = "Must be less than 150 characters")]
public string Email { get; set; }
[Required(ErrorMessage = "Password is required")]
[DataType(DataType.Password)]
[DisplayName("Password")]
[StringLength(30, ErrorMessage = "Must be less than 30 characters")]
public string LoginPassword { get; set; }
[DisplayName("Remember me")]
public bool Remember { get; set; }
}
Register Model:
public class RegisterModel
{
[Required(ErrorMessage = "Email is required")]
[RegularExpression("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*#[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2,4})$", ErrorMessage = "Not a valid email")]
[DataType(DataType.EmailAddress)]
[DisplayName("Email")]
[StringLength(150, ErrorMessage = "Must be less than 150 characters")]
public string Email { get; set; }
[Required(ErrorMessage = "Full Name is required")]
[DisplayName("Full Name")]
[StringLength(50, ErrorMessage = "Must be less than 50 characters")]
public string FullName { get; set; }
[Required(ErrorMessage = "Password is required")]
[DataType(DataType.Password)]
[DisplayName("Password")]
[StringLength(30, ErrorMessage = "Must be less than 30 characters")]
public string RegisterPassword { get; set; }
[Required(ErrorMessage = "Confirm Password is required")]
[DataType(DataType.Password)]
[DisplayName("Confirm Password")]
[StringLength(30, ErrorMessage = "Must be less than 30 characters")]
public string ConfirmPassword { get; set; }
[Required(ErrorMessage = "Please read and agree the terms and condition.")]
[DisplayName("I agree to the terms and conditions")]
public bool AgreeTerms { get; set; }
}
And they both are called on the home page using a seperate modal popups --> so lets say if a user register himself (and while registering he presses enter without entering email and validation message is displayed). After successfully registering, user tries to login (and he again presses the enter without entering email) but this time the validation message is not displayed.
As per my knowledge, the reason for not showing the validation summary is that the validation message is appearing in the register modal which is not visible at the moment and it is because both models uses an email (named : Email) field attribute.
I can achieve my desire behaviour using different name for email field, but is there any way without doing this ???
And addition to this can I inherit from Register model and use it for login purpose also, while doing this what will be the output of my above scenario?

You'd have to modify the editor templates to include a prefix or something making them unique. Here's an answer that provides some extension methods that I think would work TextBoxFor rendering to HTML with prefix on the ID attribute .

Related

Add Control to validate in ModelState manually

I have one scenario as mentioned below :
In my xyz.cshtml page, i have two form. and in xyz.cshtml.cs page i have one InputModel that contains Property for both form.
public class InputModel
{
[Required(ErrorMessage = "The Email field is required.")]
[EmailAddress(ErrorMessage = "Enter a valid email address.")]
[RegularExpression(
#"^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)#([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$",
ErrorMessage = "Enter a valid email address.")]
public string Email { get; set; }
[Required(ErrorMessage = "The Password field is required.")]
[DataType(DataType.Password)]
public string Password_ChangeEmail { get; set; }
[Required(ErrorMessage = "The OldPassword field is required.")]
[DataType(DataType.Password)]
public string OldPassword { get; set; }
[Required(ErrorMessage = "The New Password field is required.")]
[DataType(DataType.Password)]
[CustomPassword(8, 15, ErrorMessage = "The password is invalid.")]
public string Password { get; set; }
[Required(ErrorMessage = "The Confirm Password field is required.")]
[DataType(DataType.Password)]
[Compare("Password", ErrorMessage = "The passwords do not match.")]
public string ConfirmPassword { get; set; }
}
I am using first 2 property in one form and rest of the property in 2nd form.
Now, issue is when i am pressing Submit button from 1st form, it's validating 2nd form as well. So for the time i have used ModelState.Clear() . But if i am using this and i am not wrong it will stop validating all the property.
So, if you guys have any solution like, is it possible to control in ModelState manually for validate.
If i need to share anything else, please suggest.
Thanks
1) You could split the model into two, one model for each form and use them in the post actions for these forms.
public class LoginModel
{
[Required(ErrorMessage = "The Email field is required.")]
[EmailAddress(ErrorMessage = "Enter a valid email address.")]
[RegularExpression(
#"^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)#([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$",
ErrorMessage = "Enter a valid email address.")]
public string Email { get; set; }
[Required(ErrorMessage = "The Password field is required.")]
[DataType(DataType.Password)]
public string Password_ChangeEmail { get; set; }
}
public class ChangePasswordModel
{
[Required(ErrorMessage = "The OldPassword field is required.")]
[DataType(DataType.Password)]
public string OldPassword { get; set; }
[Required(ErrorMessage = "The New Password field is required.")]
[DataType(DataType.Password)]
[CustomPassword(8, 15, ErrorMessage = "The password is invalid.")]
public string Password { get; set; }
[Required(ErrorMessage = "The Confirm Password field is required.")]
[DataType(DataType.Password)]
[Compare("Password", ErrorMessage = "The passwords do not match.")]
public string ConfirmPassword { get; set; }
}
2) Create a combined model
public class InputModel
{
public LoginModel LoginModel { get; set; }
public ChangePasswordModel ChangePasswordModel { get; set; }
}
3) add two post actions
public IActionResult LoginPost(InputModel model)
{
...
return View("InputPage", model);
}
public IActionResult ChangePasswordPost(InputModel model)
{
...
return View("InputPage", model);
}
4) use the combined model in your cshtml page
#model InputModel
...
#using (Html.BeginForm("LoginPost", "Home")) {
#Html.TextBoxFor(m => m.LoginModel.Email)
<input type="submit" value="submit" />
}

How to add a repeated foreign key in the same model

I am working in an ASP MVC project, which uses MvcScaffolding a lot, I have a project and a client model, the thing is that the project has to be relationed with client model in two ways (Billing client and Service Client, and both data are from same client model)
I have no idea how to do this, here are my models
Client Model
public class ClientsX
{
[Key]
public int idcliente { get; set; }
[Required(ErrorMessage = "A Legal ID is required")]
[StringLength(150)]
[Display(Name = "Company Name: ")]
public string CompanyName { get; set; }
[StringLength(150)]
[Display(Name = "Legal ID: ")]
public string LegalID { get; set; }
[Required(ErrorMessage = "A Contact First Name is required")]
[StringLength(150)]
[Display(Name = "First Name: ")]
public string FirstName { get; set; }
[Required(ErrorMessage = "A Contact Last Name is required")]
[StringLength(150)]
[Display(Name = "Last Name: ")]
public string LastName { get; set; }
[Required(ErrorMessage = "A Contact Email Address is required")]
[DataType(DataType.EmailAddress, ErrorMessage = "Please enter a valid email address")]
[EmailAddress(ErrorMessage = "Please enter a valid email address")]
[StringLength(150)]
[Display(Name = "Email Address: ")]
public string email { get; set; }
[Required(ErrorMessage = "A Phone Number is required")]
[StringLength(15)]
[Display(Name = "Phone: ")]
public string phone { get; set; }
[Required(ErrorMessage = "An Username is required")]
[MaxLength(50, ErrorMessage = "Your Username cannot exceed 50 characters")]
[RegularExpression(#"^[\S]*$", ErrorMessage = "No White spaces are Accepted")]
[Display(Name = "Username: ")]
public string username { get; set; }
[Required(ErrorMessage = "A Password is required")]
[DataType(DataType.Password)]
[Display(Name = "Password: ")]
public string password { get; set; }
}
Project Model
public class Project
{
[Key]
public int idProject { get; set; }
[Display(Name = "Billing Client")]
[Required(ErrorMessage = "This Field is required")]
public int ClientsXId { get; set; }
[Display(Name = "Client")]
[Required(ErrorMessage = "This Field is required")]
public int ClientsXId { get; set; }
[Display(Name = "Status")]
[Required(ErrorMessage = "This Field is required")]
public int estatusId { get; set; }
}
Yes, I know ClientsXId is repeated, but is on purpose, because I need to have the same data in the project model, but it gives me this error:
Error 5 The type 'SSMS_2._0._1.Models.Project' already contains a definition for 'ClientsXId' D:\PC\Visual Studio 2013\Projects\SSMS-2.0.1\SSMS-2.0.1\Models\ProjectModel.cs 99 20 SSMS_2._0._1
How can I make this work?

'...DataType' does not contain a definition for 'Email'

I'm using Identity, and the IdentityUsers properties are the next: https://msdn.microsoft.com/en-us/library/microsoft.aspnet.identity.entityframework.identityuser_properties%28v=vs.108%29.aspx
Well, the problem is I have in the AspNetUsers table "Email" and "PasswordHash", but appears the error'System.ComponentModel.DataAnnotations.DataType' does not contain a definition for 'Email' and 'PasswordHash')
if I put "Datatype.Email" instead of "DataType.EmailAddress" and "DataType.PasswordHash" instead of "DataTtype.Password".
public class RegisterViewModel
{
[Required]
[DataType(DataType.Email)]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The password must have at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.PasswordHash)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.PasswordHash)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "Different passwords.")]
public string ConfirmPassword { get; set; }
}
The error is telling you exactly what you need. DataType is an enumeration which only allows specific values. These in turn are used for validation in the MVC world to ensure that you have a valid EmailAddress in the field, whatever it happens to be called.
[DataType(DataType.Email)] // Not Valid
[DataType(DataType.EmailAddress)] // Valid
[DataType(DataType.PasswordHash)] // Not Valid
[DataType(DataType.Password)] // Valid
For the full list of valid values, see Intellisense or this URL:
https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.datatype%28v=vs.110%29.aspx

Filtering errors in ValidationSummary

I want to Show particular errors in validation summary and disappear some errors like below:` public class LogOnModel
{
[Display(Name = "Email")]
public string Email { get; set; }
[Required(ErrorMessage = "Username must be entered.")]
[Display(Name = "Kullanıcı Adı")]
public string Username { get; set; }
[Required(ErrorMessage = "Password must be entered.")]
[DataType(DataType.Password)]
[Display(Name = "Parola")]
public string Password { get; set; }
}
How can I filter particular errors in ValidationSummary() ?

Enter conditional values either or validation in asp.net mvc2

In my form I have phone and mobile number fields I need to guarantee that the user enters at least one of the numbers. I don’t want to force the user to enter both numbers.
I looked at this previous post but it didn’t work for me
conditional either or validation in asp.net mvc2
public class ContractViewModel
{
[Required(ErrorMessage = "Please enter First Name")]
[StringLength(50, ErrorMessage = "Length of First Name must be less than 50")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Please enter Last Name")]
[StringLength(50, ErrorMessage = "Length of Last Name must be less than 50")]
public string LastName { get; set; }
[Required(ErrorMessage = "Please enter Phone Number")]
[StringLength(10,ErrorMessage = "Length of Phone No. must be less than 10")]
[RegularExpression("^[0-9]{10}$", ErrorMessage = "Please enter a valid home phone")]
public string Phone { get; set; }
[Required(ErrorMessage = "Please enter Mobile No.")]
[StringLength(10, ErrorMessage = "Length of Mobile No. must be les than 10")]
[RegularExpression("^[0-9]{10}$", ErrorMessage = "Please enter a valid mobile")]
public string Mobile { get; set; }
}
How would I go about doing this?

Resources