MVC5 date problem

lg127

Well-Known Member
Joined
Nov 8, 2010
Messages
273
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
 

skimread

Honorary Master
Joined
Oct 18, 2010
Messages
12,419
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:

lg127

Well-Known Member
Joined
Nov 8, 2010
Messages
273
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.
 

skimread

Honorary Master
Joined
Oct 18, 2010
Messages
12,419
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.
 

lg127

Well-Known Member
Joined
Nov 8, 2010
Messages
273
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!:crying:
 

Solitude

Executive Member
Joined
Jul 23, 2008
Messages
7,312
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?
 

lg127

Well-Known Member
Joined
Nov 8, 2010
Messages
273
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:

lg127

Well-Known Member
Joined
Nov 8, 2010
Messages
273
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

Executive Member
Joined
Jul 23, 2008
Messages
7,312
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. :)
 

_kabal_

Executive Member
Joined
Oct 24, 2005
Messages
5,922
is the date really showing as mm/dd/yyyy, or is it actually just adding an html placeholder of "mm/dd/yyyy"?
 

Solitude

Executive Member
Joined
Jul 23, 2008
Messages
7,312
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.
 

lg127

Well-Known Member
Joined
Nov 8, 2010
Messages
273
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)
 

Solitude

Executive Member
Joined
Jul 23, 2008
Messages
7,312
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?
 

Solitude

Executive Member
Joined
Jul 23, 2008
Messages
7,312

skimread

Honorary Master
Joined
Oct 18, 2010
Messages
12,419
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
 

lg127

Well-Known Member
Joined
Nov 8, 2010
Messages
273

lg127

Well-Known Member
Joined
Nov 8, 2010
Messages
273
Thank you very much all of you who have given advice and helped me to solve this problem :)
 
Top