I have a model with a DateTime property that in one place is placed in a hidden input field.
#Html.HiddenFor(m => m.StartDate)
Which generates the following HTML:
<input id="StartDate" name="StartDate" type="hidden" value="1/1/2011 12:00:00 AM" >
The problem is that the time is included in the value and my custom date validation expects a date in the format of ##/##/#### thus causing validation to fail. I can easily alter my custom date validation to make this situation work but I would rather make it so that the hidden field puts the value in the correct format.
I have tried using the DisplayFormat attribute on the model property but that doesn't seem to change the format of the hidden input.
I do realize that I could just create the hidden input manually and call StartDate.ToString("MM/dd/yyyy") for the value but I am also using this model in a dynamically generated list of items so the inputs are indexed and have ids like Collection[Some-Guid].StartDate which would make it a bit more difficult to figure out the id and name of the input.
Is there anyway to make the 'value' value come out in a specific format when rendering the field on the page as a hidden input?
You could use a custom editor template:
public class MyViewModel
{
[UIHint("MyHiddenDate")]
public DateTime Date { get; set; }
}
and then define ~/Views/Shared/EditorTemplates/MyHiddenDate.cshtml:
#model DateTime
#Html.Hidden("", Model.ToString("dd/MM/yyyy"))
and finally in your view use the EditorFor helper:
#model MyViewModel
#Html.EditorFor(x => x.Date)
This will render the custom editor template for the Date property of the view model and consequently render the hidden field with a value using the desired format.
Related
I'm generating Kendo's Datepicker in my MVC application.
#(Html.Kendo().DatePicker()
.Name("FileDate")
.Value(Session["FileDate"] == null ? DateTime.Now : Convert.ToDateTime(Session["FileDate"].ToString()))
.Events(e => e
.Change("datepicker_change")
)
)
When generated, I have an input field:
How can I update my code to add required attribute to an input field?
If there is a Model defined in the View, then you can decorate FileDate model field with [Required] DataAnnotation. Thus the extension will extract the data validation attributes automatically.
The second option is to add the required HTML attribute using the HtmlAttributes (or the InputHtmlAttributes I'm not sure what was the correct name). This will add the defined attributes to the corresponding HTML element.
I've got 3 custom DropDowns for DateTime model properties, each representing Day/Month/Year that set a hidden input with each value, with an also custom model binder that works fine.
I also have created a RequiredIf validation attribute with client side validation and it works fine both server and client side adding the corresponding data-val-requiredif attributes to the hidden inputs where the selected DropDown value is stored.
The problem comes when I use this RequiredIf attribute on DateTime property using the custom control.
[RequiredIf(...)]
public DateTime Start { get; set; }
and the issue is in each dropdown hidden input generated with Html.HiddenFor (binded to Start.Month in this example):
<input data-val="true" data-val-number="The field Month must be a number." id="Start_Month" name="Start.Month" type="hidden" value="">
The problem is pretty clear, the validation attribute is on the DateTime property, not it's Month subproperty, so the data-val-requiredif... validation attributes are not set on that input.
Question:
What is the best approach to make Day/Month/Year inherit the dataannotation validation attributes from it's DateTime model?
I wouldn't use data attribute names with multiple hyphens like your "data-val-number". I've also had issues with upper case so always use one hyphen after data and always lower case.
If you're passing the date back to a controller in separate pieces (i.e. month / day / year), you'll have to put it back together in a DateTime format on the server for the validation to work.
If you're putting the date together in a JSON model to pass back to the controller, MVC will convert the JSON to a model object for you. Make sure your JSON model is exactly the same as what the controller parameter is expecting.
I am working on an MVC 2.0 C# web Applciation. In one of my form, i am using LabelFor() html helper.
I am using the following syntax:
<%=Html.LabelFor(model=>model.Addedon)%>
Here, for this label i would like to associate a initial value that is DateTime.Now
I tried some thing like this:
<%=Html.LabelFor(model=>model.Addedon,new{value=DateTime.Now})%>
But, i am getting an error saying that there is no over load for this helper taking two arguments.Please help
UPDATED:
The form is create form/ add form which makes an insert operation. So, i am building a model and updating that model to the database.
In that model, i have a field called createdby. So, i need to associate this value with the username logged in and doing the insert operation.
So, how to associate this username value with the model field and i need to display as label so that it will be read only field.
Hope this makes clear..
LabelFor is only for, you guessed it, rendering a <label> element.
It also uses the [Display] and [DisplayName] attributes, so you can have a strongly-typed label with custom name.
What you're after is probably this:
<div>
<%= Html.LabelFor(model => model.Addeon) %>
</div>
<div>
<%= Html.DisplayFor(model => model.Addeon) %>
</div>
So the LabelFor will generate the property name description (e.g. 'Addeon'), while the DisplayFor will render the property value. DisplayFor can use the [DisplayFormat] attribute if you need custom formatting. You can set the default property value in the view model's constructor:
public class ViewModel
{
[Display(Name = "My awesome date")]
public DateTime Addeon {get;set;}
public ViewModel()
{
Addeon = DateTime.Now;
}
}
[EDIT]
Actually, your edit would make for a good second question instead of putting it here. Anyway, in your situation I'd create a dedicated view model that would hold the properties you need (e.g. user name) and would be filled in controller. Everything else would be conceptually the same - view would bind to the view model.
In my Model I have the following :
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:H:mm}")]
public DateTime _time { get; set; }
In my Edit View in the Textbox the value that is set is the full Date and time and when ever i try manually to edit the value through the browser the jQuery Validations Yields an error that the date format is not correct
while I'm adding ApplyFormatInEditMode=true why in the textbox I'm getting the Full Date the the formated one (only time) and why the jQuery validator throw error when the format is time only without date and how can I by pass it?
You should use Html.EditorFor and not Html.TextBoxFor if you want the custom format to be applied:
#Html.EditorFor(x => x._time)
Also by naming a property _time you are violating at least 2 C# naming conventions (property names start with an uppercase letter and not with an underscore).
Why by default were these changed when adding a new "edit" view? What are advantages when using EditorFor() vs. TextboxFor()?
I found this
By default, the Create and Edit scaffolds now use the Html.EditorFor helper instead of the Html.TextBoxFor helper. This improves support for metadata on the model in the form of
data annotation attributes when the Add View dialog box generates a view.
Quoted from here.
The advantages of EditorFor is that your code is not tied to an <input type="text". So if you decide to change something to the aspect of how your textboxes are rendered like wrapping them in a div you could simply write a custom editor template (~/Views/Shared/EditorTemplates/string.cshtml) and all your textboxes in your application will automatically benefit from this change whereas if you have hardcoded Html.TextBoxFor you will have to modify it everywhere. You could also use Data Annotations to control the way this is rendered.
TextBoxFor: It will render like text input html element corresponding to specified expression. In simple word it will always render like an input textbox irrespective datatype of the property which is getting bind with the control.
EditorFor: This control is bit smart. It renders HTML markup based on the datatype of the property. E.g. suppose there is a boolean property in model. To render this property in the view as a checkbox either we can use CheckBoxFor or EditorFor. Both will be generate the same markup.
What is the advantage of using EditorFor?
As we know, depending on the datatype of the property it generates the html markup. So suppose tomorrow if we change the datatype of property in the model, no need to change anything in the view. EditorFor control will change the html markup automatically.
The Html.TextboxFor always creates a textbox (<input type="text" ...).
While the EditorFor looks at the type and meta information, and can render another control or a template you supply.
For example for DateTime properties you can create a template that uses the jQuery DatePicker.
This is one of the basic differences not mentioned in previous comments:
Readonly property will work with textbox for and it will not work with EditorFor.
#Html.TextBoxFor(model => model.DateSoldOn, new { #readonly = "readonly" })
Above code works, where as with following you can't make control to readonly.
#Html.EditorFor(model => model.DateSoldOn, new { #readonly = "readonly" })
There is also a slight difference in the html output for a string data type.
Html.EditorFor:
<input id="Contact_FirstName" class="text-box single-line" type="text" value="Greg" name="Contact.FirstName">
Html.TextBoxFor:
<input id="Contact_FirstName" type="text" value="Greg" name="Contact.FirstName">