MVC5 date problem

You need to pass the Date property from your controller.

In your Edit HTTPGet method in the controller return the model e.g. making sure the property passed is a date

Code:
           var change= await _changeRepo.SelectById(id); //fetch entity from database
           change.DATEREQUIREDBY=DateTime.Now;//hardcode it to test
           return View("~/Views/Change/_Edit.cshtml", change);


cshtml
Code:
     @model IRMS.Models.Change @*on the first line*@
      ....
     @Html.TextBoxFor(model=>model.DATEREQUIREDBY, "{0:dd/MM/yyyy}", new { @class = "form-control" })


My controller has no HTTPGet method, only HTTPPost. See https://www.dropbox.com/s/12w8tceyjs2ozha/CHANGESController.cs?dl=0.

So I have to use @Html.TextBoxFor i.s.o. @Html.EditorFor in Edit.cshtml?

Thanks for your help so far.:p
 
My controller has no HTTPGet method, only HTTPPost. See https://www.dropbox.com/s/12w8tceyjs2ozha/CHANGESController.cs?dl=0.

So I have to use @Html.TextBoxFor i.s.o. @Html.EditorFor in Edit.cshtml?

Thanks for your help so far.:p
Use TextBoxFor. I think if you use EditorFor Chrome uses the html5 date element which behaves differently between browsers so rather force it to be a textbox

I can see you have an HttpGet
Code:
         // GET: CHANGES/Edit/5
        public ActionResult Edit(int? id)

add
Code:
change.DATEREQUIREDBY=DateTime.Now;
before the
Code:
return View(change);

for testing
 
Last edited:
Hi skimread

Thanks for your help so far. I have tried to TextBoxFor previously (and now again!) but the problem is that I loose the use of the datepicker. :confused:
I also do not need to add the code in the controller to display the record's date if I use TextBoxFor. But like I said the datepicker is then also gone.
 
Hi skimread

Thanks for your help so far. I have tried to TextBoxFor previously (and now again!) but the problem is that I loose the use of the datepicker. :confused:
I also do not need to add the code in the controller to display the record's date if I use TextBoxFor. But like I said the datepicker is then also gone.
Does the datepicker show the same in IE, Chrome and smartphone? I prefer to use a standard textbox and binding a jquery datepicker rather than use the HTML5 date control as it can vary quite a bit between browsers.
 
Does the datepicker show the same in IE, Chrome and smartphone? I prefer to use a standard textbox and binding a jquery datepicker rather than use the HTML5 date control as it can vary quite a bit between browsers.

I have tried it so far in Chrome only. I'll try again in the other browsers when I get home this afternoon. This problem is so frustrating!:cry:
 
To me it sounds like the date is null. That's also why skimread is suggesting that you specifically set the date to something to see if works.

If you add a breakpoint just before returning the view, and you look at the date in Visual Studio does it have a value?
 
The date is not null as shown in the attachment in a previous post. But I'll try your suggestion when I get home.
See: https://www.dropbox.com/s/uis79alzhwx7dqq/img01.png?dl=0
 
Last edited:
To me it sounds like the date is null. That's also why skimread is suggesting that you specifically set the date to something to see if works.

If you add a breakpoint just before returning the view, and you look at the date in Visual Studio does it have a value?

Solitude,
just a correction on my post. The date is NOT null; when I changed the html helper to TextBoxFor, the date displays but WITHOUT the datepicker functionality.
 
Solitude,
just a correction on my post. The date is NOT null; when I changed the html helper to TextBoxFor, the date displays but WITHOUT the datepicker functionality.

Ah ok.

Man, just give me the project tonight and I'll fix it for you. :)
 
is the date really showing as mm/dd/yyyy, or is it actually just adding an html placeholder of "mm/dd/yyyy"?
 
is the date really showing as mm/dd/yyyy, or is it actually just adding an html placeholder of "mm/dd/yyyy"?

I'm wondering if the datepicker he is using is setting the placeholder. That maybe he has to set the datepicker value in javascript and then it will display correctly.
 
I'm wondering if the datepicker he is using is setting the placeholder. That maybe he has to set the datepicker value in javascript and then it will display correctly.

The datepicker is autogenerated from the model where I have used DataAnnotations to set the date type as [DataType(DataType.Date)] (See post #17)
 
Which datepicker are you using?

I am not using one explicitly. It is auto created in Chrome when I annotated the field in my model as [DataType(DataType.Date)].
I am using VS2015 (C#).
 
This is what he sees

When you use EditorFor it decides itself what html element to generate e.g. <input type="date"> and this is an HTML5 date control which isn't displayed standard across browsers. I would rather use a TextBoxFor+jquery date plugin
 
Thank you very much all of you who have given advice and helped me to solve this problem :)
 
Top
Sign up to the MyBroadband newsletter
X