Results 1 to 2 of 2

Thread: MVC3, DefaultModelBinder and TypeConverter don't work simultaneously

  1. #1
    Join Date
    May 2008
    Posts
    3

    Question MVC3, DefaultModelBinder and TypeConverter don't work simultaneously

    Hi, I'm studing MVC3 and run into stupid problem. I hope you advise.

    I've class library, where my domain classes reside. This library is used in several applications.
    While they were web forms, it works perfect.
    How I did use them:

    Code:
    // Domain objects
    [TypeConverter(typeof (CompanyConverter))]
        public class Company : ValidStateDomainObject
        {
    //...
       }
    
    public class CompanyConverter : TypeConverter
        {
            
            public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
            {
                //...
            }
    
                    
            public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
            {
                // context is null for webpages and mvc projects
    
                return sourceType == typeof (string) || base.CanConvertFrom(context, sourceType);
            }
    //...
    }
    Somewhere on page.aspx I use listbox server contol which binds to the Company field via such expression:

    Code:
    BindingManager.AddBinding("ddlCompany.SelectedValue", "Employee.Company", BindingDirection.Bidirectional);
    Under the hood Binding manager calls TypeConverter to instantiate Company object from the value of listbox and binds it to the appropriate Employee.

    Then, in MVC3 app I'm trying to use DefaultModelBinder to get Company instance from HttpRequest:

    Code:
    [HttpPost]
            public ActionResult Create([Bind(Exclude = "Id")]Company company)
            {
                // company = null !!!!! if I use TypeConverter
    
                if (!ModelState.IsValid)
                    return View(company);
                
                CompanyService.SaveOrUpdate(company);
                return RedirectToAction("Index");
            }
    get null in company parameter.

    I've stepped into MVC3 source and found out that DefaultModelBinder checks if the model is ComplexType:

    Code:
    public virtual object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
    ...
     if (!bindingContext.ModelMetadata.IsComplexType) {
                    return null;
                }
    ...
    }
    
    public virtual bool IsComplexType {
                get {
                    return !(TypeDescriptor.GetConverter(ModelType).CanConvertFrom(typeof(string)));
                }
            }
    and in such case returns null instead of populated object
    when finds out that object can be converted from string!!!

    Unfortunately I can't switch off TypeConvereters in CanConvertFrom function as ITypeDescriptorContext context parameter is always null.

    So I can not use classes with typeconverters and modelbinders in MVC3 simultaneously. Though it can be very convenient.
    May be I'm doing things wrong way?

    Hope you help me,
    thank you in advance,
    Alexander.

  2. #2
    Join Date
    Jul 2010
    Posts
    245

    Default

    The BindingManager was designed to be used with WebForms rather than ASP.NET MVC so this scenario isn't supported.

    When using ASP.NET MVC (even with the 'WebForms ASPX View Engine'), databinding to controls on the form isn't supported because the .aspx pages are only used by ASP.NET MVC for their templating/HTML generation, rather than using ViewState, ControlState, etc. as in WebForms. When doing data-binding in ASP.NET MVC, you need to rely upon the full compliment of databinding infrastructure provided by ASP.NET MVC (e.g., the ModelBinder approach to receive the POST values, the Viewbag or ViewPage<T>, etc. for sending values to the page, etc.).

    Hope this helps,
    -Steve B.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •