public class ListModelBinder : System.Web.Mvc.IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
// Get the raw attempted value from the value provider
var incomingData = bindingContext.ValueProvider.GetValue("listComponent").AttemptedValue;
return incomingData;
//return incomingData.Split(new char[1] { ',' }).Select(data => TBLGRMAPCOMPONENT.Get().FirstOrDefault(o => o.Id == int.Parse(data))).ToList();
}
MVC does not know( and neither you!) how to create an Interface. What particular class should be using?
Replace with List<TBLGRMAPCOMPONENT>
More, you do not need a custom model binder - see http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
akfkmupiwu
Member
152 Points
72 Posts
IEnumerable<Object> is null in HTTPPOST, while its available at the time of get
Apr 23, 2012 03:49 PM|LINK
I have created a custom model binder
public class ListModelBinder : System.Web.Mvc.IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { // Get the raw attempted value from the value provider var incomingData = bindingContext.ValueProvider.GetValue("listComponent").AttemptedValue; return incomingData; //return incomingData.Split(new char[1] { ',' }).Select(data => TBLGRMAPCOMPONENT.Get().FirstOrDefault(o => o.Id == int.Parse(data))).ToList(); }[HandleError] protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); ModelBinders.Binders.Add(typeof(IEnumerable<TBLGRMAPCOMPONENT>), new ListModelBinder()); RegisterRoutes(RouteTable.Routes); }bindingContext.ValueProvider.GetValue("listComponent").public class ConfigurationController : Controller { private ConfigModelEFEntities db = new ConfigModelEFEntities(); public ActionResult Configuration() { DirectoryInfo directory = new DirectoryInfo(Server.MapPath(@"~\Views")); var dirs = directory.GetDirectories(); List<TBLGRMAPCOMPONENT> tblist = new List<TBLGRMAPCOMPONENT> { }; tblist = db.TBLGRMAPCOMPONENTs.ToList(); TBLGRMAPCOMPONENT temp=new TBLGRMAPCOMPONENT(); foreach (var dir in dirs) { foreach (var item in dir.GetFiles("*.cshtml")) { temp=new TBLGRMAPCOMPONENT(); temp.ISSELECTED = 0; temp.MODULENAME = dir.Name; temp.CONTROLLERNAME = temp.MODULENAME; temp.COMPONENTNAME = item.Name.Split('.').ToArray()[0]; temp.DEFAULTACTIONNAME = temp.COMPONENTNAME; tblist.Add(temp); db.AddToTBLGRMAPCOMPONENTs(temp); } } TryUpdateModel(db.TBLGRMAPCOMPONENTs); UpdateModel(db); return View(tblist); } [HttpPost] public ActionResult Configuration(IEnumerable<TBLGRMAPCOMPONENT> listComponent) //save logicignatandrei
All-Star
135047 Points
21654 Posts
Moderator
MVP
Re: IEnumerable<Object> is null in HTTPPOST, while its available at the time of get
Apr 24, 2012 06:00 AM|LINK
MVC does not know( and neither you!) how to create an Interface. What particular class should be using?
Replace with List<TBLGRMAPCOMPONENT>
More, you do not need a custom model binder - see http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx