In a recent project - i\'ve hit an unexpected snag.
A class with simple public fields (note not properties) doesn\'t seem to want to play nice with the
ASP.net MVC 3.0
Is this by design?
Yes, only properties with public getter/setters work with the default model binder.
besides changing the fields to properties - any options here?
That's what you should do as you should be using view models anyway. And view models are specifically designed for the views. So changing the fields to properties is the correct way to do. Now if you don't follow good practices and try to use your domain models as input/output to actions then you will have to write a custom model binder that works with fields. It will be a lot of work though.
I know it's against the c# team's philosophy, but I think fields are clean in poco classes. Seems like less moving parts to me. Anyway,
Here's a model binder that will load fields. It's fairly trivial. Note that you can use a new object with Activator.CreateInsance
or start with an existing object as a starting point. I was using Yes/No dropdowns for bool, you may have check boxes, in which case unfortunately you'll have to loop through fieldinfos and look for missing form inputs b/c html doesn't submit form items if it's a false checkbox.
~/Binders/FieldModelBinder.cs
using System;
using System.Reflection;
using System.Web.Mvc;
namespace MyGreatWebsite.Binders
{
public class FieldModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var obj = controllerContext.HttpContext.Session["CurrentObject"];// Activator.CreateInstance(bindingContext.ModelType);
var form = controllerContext.HttpContext.Request.Form;
foreach (string input in form)
{
FieldInfo fieldInfo = obj.GetType().GetField(input);
if (fieldInfo == null)
continue;
else if (fieldInfo.FieldType == typeof(bool))
fieldInfo.SetValue(obj, form[input] == "Yes");
else
fieldInfo.SetValue(obj, Convert.ChangeType(form[input], fieldInfo.FieldType));
}
return obj;
}
}
}
Startup.cs
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
ModelBinders.Binders.Add(typeof(FieldModelBinder), new FieldModelBinder());
}
}
Usage
[HttpPost]
public ActionResult MyGreatAction([ModelBinder(typeof(FieldModelBinder))] MyGreatProject.MyGreatNamespace.MyGreatType instance, string myGreatParameters)
{
DoSomethingGreatWithMyInstance();
return View();
}