Feb 16, 2013, 12:55 AM
[CLOSED] MVC FormPanelForModel boolean submit behaviour
Hi,
I've seen that FormPanelForModel for boolean fields, posts as value the created CheckBox's id, instead of simply true, when no instance of model is passed to the view. This causes an invalid ModelState because of wrong AttemtedValue.
For example:
let's assume we have a simple login model
Http Post of this form (if RememberMe field is checked) can be monitored in Firebug like this:
application/x-www-form-urlencoded
Parameters
Password asdf
RememberMe App.RememberMe
UserName asdf
Source
UserName=asdf&Password=asdf&RememberMe=App.Remembe rMe
As you see App.RememberMe value for boolean field will cause a false Model.IsValid
Walk-around of this issue can be one of these:
In both of these solutions you will get a true or false value on the http post, which will produce a valid ModelState.
My question is:
Is this behavior of Ext.Net.Model or FormPanelForModel classes a bug to solve, or is it a feature "as is" we need to walk-around?
You can reproduce this issue easily on the FormPanelFor example, just by removing FormPanelEmployee.GetAll()[0] parameter on the controller class.
Thanks in advance.
I've seen that FormPanelForModel for boolean fields, posts as value the created CheckBox's id, instead of simply true, when no instance of model is passed to the view. This causes an invalid ModelState because of wrong AttemtedValue.
For example:
let's assume we have a simple login model
public class LogOnModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
a simple login view@model Models.LogOnModel
@{
ViewBag.Title = "Login";
Layout = "~/Views/Shared/_BaseLayout.cshtml";
}
@(
Html.X().FormPanelForModel()
.Buttons( Html.X().Button().Text("Submit")
.DirectClickUrl(Url.Action("Submit")) )
//You can use a direct event and bind the form.
//You will get the same result.
)
and a login controllerpublic ActionResult Index()
{
return View(); //no instance of model is given to the view
}
[HttpPost]
public ActionResult Submit(Models.LogOnModel test)
{
if (Model.IsValid)
{
//Your login ticket and redirection return code should be here.
//You may can put a breakpoint before this if statement to check the ModelState.Values to see the AttemtedValue
}
return this.Direct();
}
Http Post of this form (if RememberMe field is checked) can be monitored in Firebug like this:
application/x-www-form-urlencoded
Parameters
Password asdf
RememberMe App.RememberMe
UserName asdf
Source
UserName=asdf&Password=asdf&RememberMe=App.Remembe rMe
As you see App.RememberMe value for boolean field will cause a false Model.IsValid
Walk-around of this issue can be one of these:
- giving an empty instance of the model to the view
public ActionResult Index() { return View(new Models.LogOnModel()); }
- or declaring the InputValue of boolean fields manually
@model Models.LogOnModel @( Html.X().FormPanelForModel(modelOnly:true) .Items( Html.X().TextFieldFor(m=>m.UserName), Html.X().TextFieldFor(m=>m.Password), Html.X().CheckboxFor(m=>m.RememberMe).InputValue("true").UncheckedValue("false")) .Buttons( Html.X().Button().Text("Submit") .DirectClickUrl(Url.Action("Submit")) ) )
In both of these solutions you will get a true or false value on the http post, which will produce a valid ModelState.
My question is:
Is this behavior of Ext.Net.Model or FormPanelForModel classes a bug to solve, or is it a feature "as is" we need to walk-around?
You can reproduce this issue easily on the FormPanelFor example, just by removing FormPanelEmployee.GetAll()[0] parameter on the controller class.
Thanks in advance.
Last edited by Daniil; Feb 18, 2013 at 5:31 AM.
Reason: [CLOSED]