I am very new on MVC. How can I use a calender? Here is my div
<div class="editor-field">
#Html.EditorFor(model => model.CommunicationTime)
#Html.ValidationMessageFor(model => model.CommunicationTime)
</div>
But it shows only a text box. What should I do?
try to add Data annotations DateType(DateType.Date) within your model
here is a sample :
[Display(Name = "Date of Birthday")]
[DataType(DataType.Date)]
public DateTime DOB { get; set; }
this Should work in Chrome , Firefox , Safari or any browser Supports HTML5
that's because the produced html tags will contain type="Date" in date field
Related
Does asp.net mvc client side validation using jQuery only work with views
which use HTML Helpers to generate HTML controls? Does it work with standard HTML Controls?
It is possible to use validation without turning to HtmlHelpers. See this article for details about how MVC works in the background with unobtrusive validation.
http://www.blinkingcaret.com/2016/03/23/manually-use-mvc-client-side-validation/
Though I think unless you have a very specific requirement, rather stick with Helpers, it will make your life easier.
1)Add Validation Property in model
2)In view add html control for show the validation
1)For Model
[Required(ErrorMessage = "Name is Required")]
public string Name { get; set; }
[Required(ErrorMessage = "Email is Required")]
[EmailAddress(ErrorMessage = "Invalid Email Address")]
public string Email { get; set; }
2)In View
#Html.TextBoxFor(m => m.Email)
#Html.ValidationMessageFor(m => m.Email, "", new { #class = "error" })
3)Most Important (Add the following link in the View Page)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-
validate/1.19.0/jquery.validate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validation-
unobtrusive/3.2.11/jquery.validate.unobtrusive.min.js"></script>
Okay I have searched it long enough to not find this anywhere on the net. So here is my question, in asp.net MVC (5.1) razor the default helper we can use DisplayNameFor to get the Name value from Display attribute is there anything similar for retrieving Description?
[Display(Name = "First Name", Description="What is your first name")]
public string FirstName { get; set; }
E.g.
#Html.DisplayNameFor(model=>model.FirstName)
Instead use LabelFor and try below code.
#Html.LabelFor(m => m.FirstName,
new{title=ModelMetadata.FromLambdaExpression<Models.Profile, string>
(m => m.FirstName, ViewData).Description})
In my MVC application, email validation is working perfectly locally but when I m publishing and deploying to server It won't work. I have compared the html source of both local and deployed files and there is no difference. My Razor view mark up is
<li>
<p><strong>Email: </strong>#Model.CurrentEmailAddress <span class="deliverychange" onclick="showHidden('emailchange');">(Change email)</span></p>
<div id="emailchange" class="fullborder" style="display: none;">
<div class="orderrow newemailaddress">
#Html.LabelFor(m => m.UpdatedEmailAddress)
#Html.TextBoxFor(m => m.UpdatedEmailAddress, new { #onkeypress = "showEmailChangeConfirmation();" })
#Html.ValidationMessageFor(m => m.UpdatedEmailAddress)
</div>
<div id="updatedemailkeypress" style="display: none;">
<div class="orderrow checkboxrow emailchangeconfirm">
#Html.LabelFor(m => m.UpdateEmailAddress)
#Html.EnumRadioButtonFor(m => m.UpdateEmailAddress, false)
</div>
</div>
<div class="clear"> </div>
</div>
</li>
and my model is as
[DataType(DataType.EmailAddress)]
[RegularExpression(#"^([\w.-]+)#([\w-]+)((.(\w){2,3})+)$", ErrorMessage = "Email is not valid")]
[Display(Name = "Enter new email address: ")]
public string UpdatedEmailAddress { get; set; }
[Display(Name = "We will use ****")]
public YesNo UpdateEmailAddress { get; set; }
Use the EmailAddress-attribute instead
http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.emailaddressattribute%28v=vs.110%29.aspx
Please recheck that form tag is there.
Form validation works actually by validating form valid method.
So, the form is required to be exist.
Hope this helps.
After spending a lot of time on this I found the publish wizard is not copying the jquery.validate-vsdoc.js to the scripts folder. I manually copied this to the server and all started working.
Try to use dataannotationsextensions library, you can download from nuget.
and just add Email attribute to your model like this
using DataAnnotationsExtensions;
[Required]
[DataType(DataType.EmailAddress)]
[Email]
public string UpdatedEmailAddress { get; set; }
I have a form containing a number of controls - nothing fancy:
#using (Html.BeginForm()) {
#Html.AntiForgeryToken()
<fieldset>
<legend>EmployeeViewModel</legend>
<div class="editor-label">
#Html.LabelFor(model => model.Employee.Title)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Employee.Title)
etc.....
UPDATE
My ViewModel:
public class EmployeeCreateViewModel
{
public EmployeeCreateModel Employee { get; set; }
..etc
My Employee model:
public class EmployeeCreateModel
{
[Required]
public string Title { get; set; }
[DisplayName("Job Title")]
[Required]
public string JobTitle { get; set; }
public bool Active { get; set; }
...etc
The Problem - I am using unobtrusive validation which works fine UNTIL I add a checkbox to the form. Regardless of the checkbox state, the form is submitted, bypassing client-side validation and errors are caught by the server-side validation. This is my checkbox:
<div class="editor-label">
#Html.LabelFor(model => model.Employee.Active)
</div>
<div class="editor-field">
#Html.CheckBoxFor(model => model.Employee.Active)
</div>
The checkbox model property is not a required field, so it doesn't need to be validated and I can see that it has a valid True/False value when it reaches the controller method.
Why is this happening, and how can I fix it?
First, open javascript console (e.g., chrome inspector panel) and see if you are getting Uncaught TypeError: Object [object Object] has no method 'live' error from jquery.unobtrusive-ajax.js.
If you are seeing this error, you are probably using jquery 1.9.x or higher. If you check "Preserve log upon navigation" (chrome), you can see an error saying "Uncaught SyntaxError: Unexpected token u".
To solve this problem, include jquery migrate 1.2.x after jquery 1.9.x.
<script src="/Scripts/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.js"></script>
For details, see this post.
I am currently working on a ASP.NET MVC3 project and i created views to display contents , now i want to put watermark on #html.TextBoxFor() so i tried [Display(Prompt="WaterMarkText")],but its not working in IE8 and working well in Google chrome
Please help me to put watermark ..
here is my code
[Display(Prompt = "Scheme", Name = "Scheme")]
[Required(ErrorMessage = "*")]
public string name { get; set; }
view
<td>
#Html.LabelFor(model => model.scheme.name)
</td>
<td>
#Html.EditorFor(model => model.scheme.name)
</td>
<td>
#Html.ValidationMessageFor(model => model.scheme.name)
</td>
The Prompt parameter in the Display attribute just adds a "placeholder" attribute to the rendered input tag, like this:
<input type="text" placeholder="WaterMarkText" />
IE8 doesn't support the placeholder attribute, so you need to use javascript or CSS to achieve the same. You can do it with jQuery and this: http://www.standardista.com/html5-placeholder-attribute-script/