PDA

View Full Version : [CLOSED] Model DateField on submit returns null



ingbabic
Sep 29, 2015, 3:51 PM
Hello
What could be a problem when DateField won't bind from form? If I have for example view like this:


@model TestSubmit.Models.MyObject

@(Html.X().Window().Width(450).Title("My object")
.Items(
Html.X().FormPanel().AutoDataBind(true).ID("FormPanel1").Frame(true).DefaultAnchor("100%").BodyPadding(6)
.Items(
Html.X().HiddenFor(m=>m.IDProperty),
Html.X().TextFieldFor(m => m.StringProperty).ID("firstControl").SelectOnFocus(true),
Html.X().CheckboxFor(m=> m.BooleanProperty),
Html.X().DateFieldFor(m=> m.DateProperty),
Html.X().TextFieldFor(m=> m.IntegerProperty),
Html.X().TextFieldFor(m=> m.FloatProperty)
)
.Buttons(
Html.X().Button().ID("OK").Text("Ok").Icon(Icon.Disk)
.DirectEvents(de =>
{
de.Click.Url = Url.Action("Submit");
de.Click.EventMask.ShowMask = true;
de.Click.FormID = "FormPanel1";
de.Click.Success = "this.up('window').close();";
})
,
Html.X().Button()
.Text("Cancel")
.OnClientClick("this.up('window').close();")
)
)
)


On action Submit:


public ActionResult Submit(MyObject myObject)


I get DateProperty as null. All other properties are bound well...

Daniil
Sep 29, 2015, 5:13 PM
Hi @ingbabic,

Well, it is either not sent from a client (or wrong value) or it cannot be deserialized on server. But why it happens I don't quite have any at the moment.

This example looks to be the same/similar as your setup and it works well.
http://mvc2.ext.net/#/Models/Submit

Could you, please, check in a request if a DateField's is being submitted or not?

ingbabic
Sep 30, 2015, 7:37 AM
Here is what I see in Firebug console for submit parameters:


BooleanProperty
false
DateProperty
1.10.2015.
FloatProperty
345,2
IDProperty
327dabf4-8aee-453d-8e6d-2cd4d6b6f93b
IntegerProperty
11
StringProperty
ingbabic
Source
IDProperty=327dabf4-8aee-453d-8e6d-2cd4d6b6f93b&StringProperty=ingbabic&BooleanProperty=false&DateProperty
=1.10.2015.&IntegerProperty=11&FloatProperty=345%2C2

Daniil
Sep 30, 2015, 1:00 PM
Okay, the date is being submitted.

Please post the Model class.

ingbabic
Oct 01, 2015, 7:39 AM
Here is the modela class:


public class MyObject
{
private static List<MyObject> _allObjects;

static MyObject()
{
_allObjects = new List<MyObject>()
{
new MyObject(){ IDProperty=Guid.NewGuid(), StringProperty = "string1", BooleanProperty = false, DateProperty=DateTime.Now, FloatProperty=123.2F, IntegerProperty=18},
new MyObject(){ IDProperty=Guid.NewGuid(), StringProperty = "string2", BooleanProperty = true, DateProperty=DateTime.Now.AddDays(-1), FloatProperty=234.2F, IntegerProperty=19},
new MyObject(){ IDProperty=Guid.NewGuid(), StringProperty = "string3", BooleanProperty = false, DateProperty=DateTime.Now.AddDays(1), FloatProperty=345.2F, IntegerProperty=11},
new MyObject(){ IDProperty=Guid.NewGuid(), StringProperty = "string4", BooleanProperty = true, DateProperty=DateTime.Now.AddDays(-2), FloatProperty=456.2F, IntegerProperty=13},
new MyObject(){ IDProperty=Guid.NewGuid(), StringProperty = "string5", BooleanProperty = false, DateProperty=DateTime.Now.AddDays(-3), FloatProperty=567.2F, IntegerProperty=15}
};
}

public Guid IDProperty { get; set; }
[Required(ErrorMessage="This may not be empty")]
public string StringProperty { get; set; }
[DefaultValue(false)]
public bool BooleanProperty { get; set; }
public DateTime? DateProperty { get; set; }
[DefaultValue(0)]
public int? IntegerProperty { get; set; }
[DefaultValue(0)]
public float? FloatProperty { get; set; }

public static List<MyObject>GetAll()
{
return _allObjects;
}
}


I have impression that it has to do something with regional settings and date. I am trying to run this sample on Windows 10 (fresh installed), and on old Windows 8.1 it is working.

Daniil
Oct 01, 2015, 11:13 AM
I have impression that it has to do something with regional settings and date.

Probably, it might be.

Please post the Page Sources (Ctrl+U in a browser).

Also do you have any locale setting in Web.config?

Please try to add this into Web.config and retest. Is the issue still reproducible?

<globalization culture="en-US" />

ingbabic
Oct 01, 2015, 1:46 PM
Please try to add this into Web.config and retest. Is the issue still reproducible?

<globalization culture="en-US" />


Well, that was it. Interesting that it was not needed in Windows 8.1, and in Windows 10, without this setting it couldn't work.
Thank you very much :)

Daniil
Oct 01, 2015, 2:09 PM
Could you, please, still post the Page Sources when there is no such setting? Maybe, I will be able to understand what is going wrong.

ingbabic
Oct 01, 2015, 3:45 PM
Sure I can.
Just when I should press ctrl-U and take code? Full sample is having a grid panel filled with data from model. On grid panel I have a context menu, where I open a window form with data from selected row. Problem occurs when I press OK on that form (it should update model data and selected row in grid). So when I should pres ctrl-U? (see screenshot)

24256

Daniil
Oct 01, 2015, 8:43 PM
Just when I should press ctrl-U and take code?

Please do that when the page is loaded into a browser. In my understanding any actions on a page doesn't affect its sources.

ingbabic
Oct 02, 2015, 8:15 AM
Here it is:


<!DOCTYPE html>

<html>
<head>
<link type="text/css" rel="stylesheet" href="/TestSubmit/extjs/resources/ext_theme_gray/ext-theme-gray-all-embedded-css/ext.axd?v=10552" id="ext-theme" />
<link type="text/css" rel="stylesheet" href="/TestSubmit/extnet/resources/css/extnet-all-embedded-css/ext.axd?v=10552" id="extnet-styles" />
<script type="text/javascript" src="/TestSubmit/extjs/ext-all-js/ext.axd?v=10552"></script>
<script type="text/javascript" src="/TestSubmit/extnet/extnet-all-js/ext.axd?v=10552"></script>

<title>Index</title>
<link rel="stylesheet" href="/TestSubmit/resources/css/examples.css" />

<style>
.my_icon {
background-image:url(../../Resources/EditTableHS.png)
}
</style>
<script>
Ext.onReady(function ()
{

Ext.get(window.document).on('contextmenu', function (e)
{
e.preventDefault();
return false;
});

});

function reloadGridPanel(grid, data) {
grid.show();
grid.getStore().loadData(data);
};


var onSuccessEdit = function (data, cmd) {
switch (cmd) {
case "edit":
var record = App.objectsGrid.getSelectionModel().selected.items[0];
var index = App.objectsGrid.getStore().indexOf(record);
var r = App.objectsGrid.getStore().getAt(index);
r.beginEdit();
r.data = data;
r.endEdit();
r.commit();
break;
case "addnew":
var numRow = App.objectsGrid.getStore().getCount();
App.objectsGrid.getStore().insert(numRow, data);
break;
case "delete":
App.objectsGrid.deleteSelected();
break;
}
};


var template = '<span style="color:{0};">{1}</span>';

var change = function (value) {
return Ext.String.format(template, (value) ? "green" : "red", value);
};

var tpl = "<img src='{0}' />";

var booleanRenderer = function (value) {
if (value == null || !value)
return "<img src=\"http://localhost/TestSubmit/Resources/cross.png\" />";
else
return "<img src=\"http://localhost/TestSubmit/Resources/tick.png\" />";
}
</script>


<script type="text/javascript">
//<![CDATA[
Ext.net.ResourceMgr.init({isMVC:true,theme:"gray",appName:"TestSubmit"});Ext.onReady(function(){Ext.ns("App.direct");Ext.apply(App.direct, { DoYes:function(id,config){return Ext.net.DirectMethod.request("DoYes",Ext.applyIf(config || {}, {params:{id:id},url:"/TestSubmit/Home/DoYes"}));} });Ext.create("Ext.grid.Panel",{store:{model:Ext.define(Ext.id(), {extend: "Ext.data.Model", fields:[{name:"IDProperty"},{name:"StringProperty"},{name:"BooleanProperty"},{name:"DateProperty"},{name:"IntegerProperty"},{name:"FloatProperty"}] }),storeId:"Store1",autoLoad:true,proxy:{data:[{"IDProperty":"123b60cf-6d9c-48d6-ac25-306724e7fd35","StringProperty":"string1","BooleanProperty":false,"DateProperty":"2015-10-02T09:08:22.7648922","IntegerProperty":18,"FloatProperty":123.2},{"IDProperty":"f9912604-f8ca-48fc-a97d-f6ba65b1b2c6","StringProperty":"string2","BooleanProperty":true,"DateProperty":"2015-10-01T09:08:22.7668916","IntegerProperty":19,"FloatProperty":234.2},{"IDProperty":"1393f792-0625-46f9-8b58-18c99bdc74ed","StringProperty":"string3","BooleanProperty":false,"DateProperty":"2015-10-03T09:08:22.7668916","IntegerProperty":11,"FloatProperty":345.2},{"IDProperty":"084e797e-6e74-4bf6-b296-b9b878bdde29","StringProperty":"string4","BooleanProperty":true,"DateProperty":"2015-09-30T09:08:22.7668916","IntegerProperty":13,"FloatProperty":456.2},{"IDProperty":"b65bb38a-0dc9-40b7-ad52-4883744eb0fb","StringProperty":"string5","BooleanProperty":false,"DateProperty":"2015-09-29T09:08:22.7668916","IntegerProperty":15,"FloatProperty":567.2}], type: 'memory'}},id:"objectsGrid",renderTo:"App.objectsGrid_Container",tbar:{xtype:"toolbar",items:[{iconCls:"#TableAdd",text:"Add object",directEvents:{click:{fn:function(item,e){Ext.net. directRequest({cleanRequest:true,url:"/TestSubmit/Home/AddObject",control:this});}}}},{iconCls:"#TableRefresh",text:"Refresh",directEvents:{click:{fn:function(item,e){Ext.net. directRequest({cleanRequest:true,url:"/TestSubmit/Home/Refresh",userSuccess:function(response,result,el,type,acti on,extraParams,o){reloadGridPanel(App.objectsGrid, result.data);},control:this});}}}},{iconCls:"#ImageAdd",text:"Import image",directEvents:{click:{fn:function(item,e){Ext.net. directRequest({cleanRequest:true,url:"/TestSubmit/Home/ImportImage",control:this});}}}}]},title:"MyObjects",columns:{items:[{width:300,dataIndex:"StringProperty",text:"Стринг проперти"},{width:300,dataIndex:"BooleanProperty",renderer:booleanRenderer,text:"BooleanProperty"},{width:300,xtype:"datecolumn",dataIndex:"DateProperty",text:"DateProperty",format:"j.n.Y."},{width:300,dataIndex:"IntegerProperty",text:"IntegerProperty"},{width:300,dataIndex:"FloatProperty",text:"FloatProperty"}]},listeners:{itemcontextmenu:{fn:function(item,rec ord,node,index,e){e.preventDefault();App.ItemConte xtMenu.dataRecord=record.data;App.ItemContextMenu. showAt(e.getXY());}},viewready:{delay:100,fn:funct ion(item){this.selModel.select(0);this.getView().f ocusCell({row:0, column:0});}}},directEvents:{itemdblclick:{fn:func tion(item,record,node,index,e){Ext.net.directReque st({cleanRequest:true,url:"/TestSubmit/Home/EditObject",extraParams:{"objectId":record.data.IDProperty},control:this,action:'Item DblClick'});}}}});Ext.create("Ext.menu.Menu",{id:"ItemContextMenu",items:[{iconCls:"#TableEdit",text:"Edit object",directEvents:{click:{fn:function(item,e){Ext.net. directRequest({cleanRequest:true,url:"/TestSubmit/Home/EditObject",extraParams:{"objectId":this.parentMenu.dataRecord.IDProperty},control:th is});}}}},{iconCls:"#Lightning",text:"Switch boolean property",directEvents:{click:{fn:function(item,e){Ext.net. directRequest({cleanRequest:true,url:"/TestSubmit/Home/SwitchBool",extraParams:{"objectId":this.parentMenu.dataRecord.IDProperty},control:th is});}}}},{xtype:"menuseparator"},{iconCls:"#TableDelete",text:"Delete object",directEvents:{click:{fn:function(item,e){Ext.net. directRequest({cleanRequest:true,url:"/TestSubmit/Home/DeleteObject",extraParams:{"objectId":this.parentMenu.dataRecord.IDProperty},control:th is});}}}}]});Ext.net.ResourceMgr.registerIcon(["TableAdd","TableRefresh","ImageAdd","TableEdit","Lightning","TableDelete"]);});
//]]>
</script>
</head>
<body>


<div id="App.objectsGrid_Container"></div>



</body>
</html>

Daniil
Oct 03, 2015, 12:59 PM
Ok, I just got that a FormPanel is being rendered on the fly as a partial view. I tried such a scenario and, seems, it didn't affect on it - I still cannot reproduce. Could you, though, post a DirectEvent's response which renders the partial view with DateFieldFor? In particular, I am mostly interested in a DateField configuration script.

Also I've just noticed a trailing dot in the DateField in your screenshot. Do you know where it comes from? Maybe, just accidentally typed?

P.S. Please disregard my questions if you don't want to spend time on that anymore and you are okay to define this a solution.
<globalization culture="en-US" />

ingbabic
Oct 05, 2015, 8:37 AM
Daniil
Sure I want help as much as its needed, no problem. But before go on deep dive, once again I want to underline that this is happening only on Windows 10. Trailing dot is not accidentally typed, it is a way how Microsoft sees regional settings for Serbia (for the first time). Somehow I have a feeling that this problem comes from that side, as some other programs as well won't work as before. Please see the picture:

24257

Not only that they put dot at the end of the year, but they also put dot in time (instead of colon, as it was for Serbian regional settings, since windows began). I don't need to mention that of course we don't put trailing dot at the end of the year, and dot between hours minutes and seconds and I simply can't understand why they changed it that way.

Ok, back to business now.


Could you, though, post a DirectEvent's response which renders the partial view with DateFieldFor? In particular, I am mostly interested in a DateField configuration script.

How can I do this?

Daniil
Oct 05, 2015, 1:46 PM
Thank you for the details!

Yes, it looks like that a default MVC DateTime model binder cannot parse a date string from a request. Personally, I am not sure why it happens. Maybe, a culprit is a trailing dot indeed, but I cannot prove it. It might be difficult to prove without a possibility to reproduce it locally.

Please try to run this non-Ext.NET sample on Windows 10 where the original issue happens.

Model

public class Entity
{
public DateTime? LaunchDate { get; set; }

public static Entity GetEntitity()
{
Entity person = new Entity
{
LaunchDate = DateTime.Today
};

return person;
}
}

View

@model Work2MVC.Controllers.Entity

<!DOCTYPE html>
<html>
<head>
<title>Ext.Net.MVC v2 Example</title>

</head>
<body>
<form action="@Url.Action("Submit")" method="post">
@Html.EditorFor(m => m.LaunchDate)

<input type="submit" value="Submit">
</form>
</body>
</html>


Controller

public ActionResult Index()
{
return View(Entity.GetEntitity());
}

public ActionResult Submit(Entity entity)
{
return this.Json(entity.LaunchDate.ToString());
}

Please post what you see in a browser after initial page load and after pressing the Submit button.

For me I see the dates in the format according to my Windows regional settings.


Could you, though, post a DirectEvent's response which renders the partial view with DateFieldFor? In particular, I am mostly interested in a DateField configuration script.

How can I do this?



It is in a request' response. You can inspect requests and their response using a browser's developer tools (usually, a Network tab).

ingbabic
Oct 06, 2015, 12:24 PM
Daniil
Somehow i can't reproduce this problem anymore. Even if I remove <globalization culture="en-US" />, it works without problem (your sample works as expected too). I don't know what I have done. I played a bit with regional settings on my computer, but even after I reset all settings it still works. Thank you for support and sorry for wasting your time.

Daniil
Oct 06, 2015, 1:11 PM
Glad to hear it is not reproducible anymore. Maybe, some Windows update fixed that? Well, never mind. If the issue re-appear, you could post a follow-up in this thread without starting a new forum thread.