PDA

View Full Version : [CLOSED] MVC FormPanelForModel boolean submit behaviour



cankut84
Feb 16, 2013, 12:55 AM
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


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 controller


public 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.RememberMe

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 (http://mvc.ext.net/#/Models/FormPanelFor/), just by removing FormPanelEmployee.GetAll()[0] parameter on the controller class.

Thanks in advance.

Vladimir
Feb 17, 2013, 7:58 PM
Hi,

Thanks for the report, fixed in SVN

cankut84
Feb 17, 2013, 9:36 PM
I've acquired latest files from SVN trunk. Problem solved. You may can mark this thread as closed.

Thanks for your interest.

.::IT::.
Apr 22, 2013, 4:23 AM
I've acquired latest files from SVN trunk. Problem solved. You may can mark this thread as closed.

Thanks for your interest.

There anything we can do we are not premium member? : (

Daniil
Apr 22, 2013, 5:49 AM
Ext.NET v2.2 has been released last week. It includes that fix.

There is the NuGet Ext.Net.MVC package.
https://nuget.org/packages/Ext.NET.MVC/

It is the simplest way to get the release.