[StringLength(255, MinimumLength=6,
ErrorMessage = "Password can't be less that 6 characters"),
Required,
DataType(DataType.Password),
Display(Name = "Password")]
public string Password { get; set; }
[StringLength(255, MinimumLength = 6), Required, DataType(DataType.Password),
Compare("Password"), Display(Name = "Repeat password")]
public string RepeatPassword { get; set; }
If user enters password less than 6 & passwords don't match. Error message is :
Password can't be less that 6 characters 'Repeat password' and
'Password' do not match.
if repeat password is less than 6 & passwords do not match error is:
'Repeat password' and 'Password' do not match. The field Repeat
password must be a string with a minimum length of 6 and a maximum
length of 255.
if this is the case I only want 'Repeat password' and 'Password' do not match to appear. How can I do this?
Related
in my application I query EF code first with linq to entities.
My db context contains two entities Application and Status
class Application
{
public string Id {get; set;}
public string DossierId {get; set;}
public virtual Dossier {get; set;}
public DateTimeOffset CreatedDate {get; set;}
public string LastStatusId {get; set;}
public virtual Status LastStatus {get; set;}
}
class Status
{
public string Id {get; set;}
public string Name {get; set;}
}
This is my query
var dateDiff = DateTime.Now.AddMonths(-12);
var retVal = db.Applications
.Where(app => app.CreatedDate > dateDiff)
.GroupBy(app => app.DossierId,
app => app,
(dosId, app) => app.OrderByDescending(t => t.CreatedDate).FirstOrDefault())
.Select(app => new GridData()
{
Id = app.Id,
ActualState = app.LastStatus.Name,
ApplicationDate = app.CreatedDate
})
.OrderBy(app=>app.ApplicationDate);
Then I page the result with a page size of 30.
My performance problem depends on the ActualState navigaton property: if I comment it out the execution is very fast (less than a second) while if I query ActualState the execution is very slow as it enumerated all the records (ten seconds).
Have you got any hints?
Thanks,
Alessandro
EDIT
I forgot to mention that Proxy is enabled and that I have a custom mapping relation between Status and Application
this.HasRequired(app => app.LastStatus)
.WithMany()
.HasForeignKey(app => app.LastStatusId)
.WillCascadeOnDelete(false);
Try to include the Status via eager loading
db.Applications.Include(p=>p.LastStatus)
Your updated code:
var retVal = db.Applications.Include(p=>p.LastStatus)
.Where(app => app.CreatedDate > dateDiff)
.GroupBy(app => app.DossierId,
app => app,
(dosId, app) => app.OrderByDescending(t => t.CreatedDate).FirstOrDefault())
.Select(app => new GridData()
{
Id = app.Id,
ActualState = app.LastStatus.Name,
ApplicationDate = app.CreatedDate
})
.OrderBy(app=>app.ApplicationDate);
I need your help. I am working with MVC3-Razor application. I need to validate a textbox on View (.cshtml file) in such a way that, the starting 2 characters must be "PR" and 4th character must be "2". This is the requirement. How would i achieve this functionality? Any suggestions, it would be great help. Thanks for your precious time.
Model
public class RegisterModel
{
public int ID { get; set; }
[RegularExpression(#"^PR[a-zA-Z0-9]2([a-zA-Z0-9]*)$", ErrorMessage = "Please enter valid Name.")]
[Required(ErrorMessage = "Name is required.")]
public string Name { get; set; }
}
View
#using (Html.BeginForm("DYmanicControllerPage", "Test", FormMethod.Post, new { id = "FrmIndex" }))
{
<div>
#Html.LabelFor(m => m.Name)
#Html.TextBoxFor(m => m.Name)
#Html.ValidationMessageFor(m => m.Name)
</div>
}
I am using ASP.NET MVC 3 TextBoxFor in a form and would like to use type="email" for easier input for at least some mobile devices but cannot find how to set it using TextBoxFor. Is this not easily possible?
In View
#Html.LabelFor(m => m.Email)
#Html.TextBoxFor(m => m.Email)
In model
[StringLength(50)]
public string Email { get; set; }
(Already using a data annotation to protect size constraint in DB)
Try to use
#Html.TextBoxFor(m => m.Email, new { #type = "email" })
http://msdn.microsoft.com/en-us/library/ee703538.aspx (htmlAttributes)
You're using it the wrong way.
Use EditorFor in View:
#Html.LabelFor(m => m.Email)
#Html.EditorFor(m => m.Email)
In model, add the [DataType( DataType.EmailAddress )] Data Annotations property:
[DataType( DataType.EmailAddress )]
public string Email { get; set; }
Try adding [DataType( DataType.EmailAddress )] to the email property.
[DataType( DataType.EmailAddress )]
[StringLength(50)]
public string Email { get; set; }
[StringLength(50)]
[DataType(DataType.EmailAddress, ErrorMessage = "Invalid Email Address")]
public string EmailAddress { get; set; }
Try to add this. I think it works.
I have a class marked as follows:
public class MyClass{
[Display(Name="First Name")]
public string FirstName{get;set;}
}
In the Razor view I am accessing it like so, where MyClass is a property on the model:
#Html.Label("MyClass.FirstName")
However the value defined in the Display attribute isn't displayed. If I write:
#Html.LabelFor(model => model.MyClass.FirstName)
This works fine, however for the solution I am working on I have to use the first method. What have I missed on the first method?
UPDATE
Thanks for looking at this question, the problem was caused by the model be altered before the partial view was called. This mean that the model being evaluated against was not the model I was expecting.
The problem is now resolved.
If you are strongly typing your view with the MyClass model try
#Html.LabelFor(model => model.FirstName)
Here is an example from one of my projects.
First the view model class
public class UsersRegisterViewModel
{
[Display(Name = "E-Mail Address")]
[Required(ErrorMessage = "E-Mail address is required")]
[Email(ErrorMessage = "Not a valid e-mail address")]
[Remote("UserNameIsAvailable", "Validation", ErrorMessage = "Username is not available")]
public string UserName { get; set; }
[Display(Name = "Password")]
[Required(ErrorMessage = "Please enter a password")]
public string Password { get; set; }
[Display(Name = "Verify Password")]
[Required(ErrorMessage = "Please confirm your password")]
[Compare("Password", ErrorMessage = "Passwords don't match")]
public string VerifyPassword { get; set; }
[Required(ErrorMessage = "Please enter a display name")]
[Remote("DisplayNameIsAvailable", "Validation", ErrorMessage = "Display name is not avalable")]
public string DisplayName { get; set; }
}
Now the View (Ignore the AJAX goo)
#model UsersRegisterViewModel
<div id="user-register" class="site-contol">
<h2>Account Registration</h2>
<p></p>
#using (Ajax.BeginForm("Register", "Users", null, new AjaxOptions
{
HttpMethod = "post",
UpdateTargetId = "user-registration",
InsertionMode = InsertionMode.Replace,
OnSuccess = "registrationCallBacks.onSuccess",
OnFailure = "registrationCallBacks.onError"
}, new {#id = "frm-sign-in"}))
{
<ul>
<li>#Html.LabelFor(m => m.UserName)</li>
<li>#Html.TextBoxFor(m => m.UserName) #Html.ValidationMessageFor(m => m.UserName)</li>
<li>#Html.LabelFor(m => m.Password)</li>
<li>#Html.PasswordFor(m => m.Password) #Html.ValidationMessageFor(m => m.Password)</li>
<li>#Html.LabelFor(m => m.VerifyPassword)</li>
<li>#Html.PasswordFor(m => m.VerifyPassword) #Html.ValidationMessageFor(m => m.VerifyPassword)</li>
<li>#Html.LabelFor(m => m.DisplayName)</li>
<li>#Html.TextBoxFor(m => m.DisplayName) #Html.ValidationMessageFor(m => m.DisplayName)</li>
<li>
<ul>
<li><input type="submit" name="sb-register" value="Create Account"/></li>
</ul>
</li>
</ul>
}
</div>
This is very basic but it always returns false on the compare validation. Anyone else running in to this problem?
public class UsersRegisterUserViewModel
{
[DisplayName("E-Mail Address")]
[Required(ErrorMessage = "E-Mail Address is required")]
[RegularExpression(#"^[A-Za-z0-9_\-\.]+#(([A-Za-z0-9\-])+\.)+([A-Za-z\-])+$", ErrorMessage = "Invalid E-mail Address")]
public string RegUsername { get; set; }
[Required]
[Display(Name = "Password")]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required]
[Display(Name = "Confirm Password")]
[Compare("Password", ErrorMessage = "Passwords must match")]
[DataType(DataType.Password)]
public string RegConfirmPassword { get; set; }
}
adapters.add("equalto", ["other"], function (options) {
var prefix = getModelPrefix(options.element.name),
other = options.params.other,
fullOtherName = appendModelPrefix(other, prefix),
//element = $(options.form).find(":input[name=" + fullOtherName + "]")[0];
element = $(options.form).find(":input[name='" + fullOtherName + "']")[0];
MVC3 Compare attribute is buggy when comparing passwords independently of Account Controller. It seems it is hardcoded to only work with Account controller.
1. Cut and past email, password, confirm password from RegisterModel into a new file called ViewModels/ShortRegister.cs
2. Cut razor code ( email, password, confirm password) from register view and past it into partial view, call it "_shortRegistration".
3. Create a new controller called "ShortRegistration". Add the partial view into ShortRegistation.
5. Add related jquery scripts
Create a link on home page to ShortRegistration.
Confirmation error message always fires error message.
Remove the email from the partial view confirmation, The Compare functionality works.
Add userName to the partial view and view-model, Compare functionality fails, again password confirmation error message always displays error message.
Has this bug been fixed? I disabled Compare attribute and wrote jquery and CCS to fix this! I am more than happy to email the code to prove that Compare is buggy.
Hmm, no, I am not running into such problems. I've just tested the following code and it worked perfectly fine as expected.
Model:
public class UsersRegisterUserViewModel
{
[DisplayName("E-Mail Address")]
[Required(ErrorMessage = "E-Mail Address is required")]
[RegularExpression(#"^[A-Za-z0-9_\-\.]+#(([A-Za-z0-9\-])+\.)+([A-Za-z\-])+$", ErrorMessage = "Invalid E-mail Address")]
public string RegUsername { get; set; }
[Required]
[Display(Name = "Password")]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required]
[Display(Name = "Confirm Password")]
[Compare("Password", ErrorMessage = "Passwords must match")]
[DataType(DataType.Password)]
public string RegConfirmPassword { get; set; }
}
Controller:
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new UsersRegisterUserViewModel();
return View(model);
}
[HttpPost]
public ActionResult Index(UsersRegisterUserViewModel model)
{
return View(model);
}
}
View:
#model UsersRegisterUserViewModel
<script src="#Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
#using (Html.BeginForm())
{
<div>
#Html.LabelFor(x => x.RegUsername)
#Html.EditorFor(x => x.RegUsername)
#Html.ValidationMessageFor(x => x.RegUsername)
</div>
<div>
#Html.LabelFor(x => x.Password)
#Html.EditorFor(x => x.Password)
#Html.ValidationMessageFor(x => x.Password)
</div>
<div>
#Html.LabelFor(x => x.RegConfirmPassword)
#Html.EditorFor(x => x.RegConfirmPassword)
#Html.ValidationMessageFor(x => x.RegConfirmPassword)
</div>
<input type="submit" value="OK" />
}
So now the question becomes: how is your code different than mine?