What's the best way to do that?
When i bind the value of the input element to my DateTime instance, a String is returned:
<input value="{{myDate}}" type="date">
Is there a specific attribute to bind the input to a DateTime instance?
Or should i use a filter?
[EDIT]
here is my working filter (only works with "date" input type):
https://gist.github.com/Vloz/10553552
input elements don't provide an attribute that provides the typed value of the input. You'll need to provide a custom handler that parses the String value and updates your element's DateTime typed attribute.
e.g.
<input type="date" value="{{dateString}}">
...
// inside the element's script...
date: null, // the DateTime typed attribute
// this handler automatically works
dateStringChanged: function(oldValue, newValue) {
this.date = DateTime.parse(newValue); // how about some error handling too?
},
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 need to know that how to bind value in html text box in mvc from database.
i am using datepicker in that input type textbox.
<input type="text" name="objmember.BirthDate" data-beatpicker="true" class="span4" />
i am able to submit the value from this textbox in database field but while edit it i need to already show the date in this input type text box.on the other hand other textbox are showing the value fine.
#Html.TextBoxFor(m => m.objmember.Name, new { placeholder = "Name", #class = "span4" })
i am using viewModel in this view.but writing name="objmember.BirthDate" is not binding the value with this textbox.
above default textbox of mvc works fine with it.but on using input type text it is not binding the value. i have to use this datepicker in input type field because it is not working with #Html.Textbox.
Please give me some solution to this problem.
Can I configure the HTML Helper to display String.Empty if my date is MinValue?
${Html.TextBoxFor(v=>v.Date)}
How can I do this?
Why don't you do it in your View?
Caution: Razor syntax
#if(v.Date != DateTime.MinValue)
{
Html.TextBoxFor(v => v.Date);
}
else {
//show whatever
}
There are a few similar questions like
Html.TextboxFor default value for Integer/Decimal to be Empty instead of 0
Display empty textbox using Html.TextBoxFor on a not-null property in an EF entity
If you have full control over the viewmodel, one option would be to make the date field nullable and have the accessor return null if the date is equal to DateTime.MinValue.
If you don't want to change your viewmodel, you can write your own html helper and put the logic to create an empty textbox there. That allows you to keep logic out of your view, where it's difficult to test.
Make your property nullable and it won't default to the min date. If that's a database field and you can't do that then you should create a ViewModel property that is and have it compare against the min value and return null in the get{}
I have created a Listbox in which one column can contain different components like Combobox, Datebox, Decimal box etc. In One column I have to display Datebox or Decimalbox on some condition so i bind Datebox and Decimalbox inside this column with String value. I am getting exception when I bind string value to datebox and try to enter the date using popup calender.
<datebox value="#bind(data.prodValue)" width="250px"></datebox>
private String prodValue;
prodValue is of String type.When I enter the date in Datebox, I am getting exception.
Can anybody tell me how to use Datebox with string binding value?
you can create customize datebox where datatype String and Timestamp condition wize .
just set flag type="String/Timestamp"
find this class here just extend in new customize class override setValue method and modify getValue method
datebox use java.util.Date
if you bind #bind(data.prodValue) will be invoked getProdValue() method
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.