4
Answers

How to assign current date to strongly typed date property in a view?

How to assign current date to strongly typed date property in a view in asp.net mvc 5?

I have tried

 

and 

@Html.TextBoxFor(model=>model.Dated,new {@class="form-control",@type="date", @value = DateTime.Now.ToString("MM/dd/yyyy") })

and also tried from controller

but these are not working

Please help me...

Answers (4)

4
Photo of Sachin Singh
NA 55.8k 89.2k 3y
"V" In value should be capital, @Value. Now test the same.
2
Photo of Sachin Singh
NA 55.8k 89.2k 3y
Other methods
Method 1.
  1.      
  2. @Html.TextBoxFor(x=> x.Dated, new { @Value = @DateTime.Now.ToShortDateString() })  
 method 2.
  1. @{    
  2. current=System.DateTime.Now.ToString()     
  3. }     
  4. @Html.TextBoxFor(m => m.Dated, "{0:d MMM yyyy}"new { @class="input-large",@Value=current })    
1
Photo of Pravinkumar Birajdar
967 696 114.6k 3y
Sorry this is also not working,
 
I have tried
 
@{
string current = DateTime.Now.ToString();
}
 
@Html.TextBoxFor(model=>model.Dated, "{0:d MMM yyyy}", new {@class="form-control",@type="date", @Value = current })
 
but its not working 
1
Photo of Pravinkumar Birajdar
967 696 114.6k 3y
Hello Sachin,
I have tried capital V
 
@Html.TextBoxFor(model=>model.Dated,new {@class="form-control",@type="date", @Value = DateTime.Now.ToString("MM/dd/yyyy") })
 
but still it is not working