title: Form To View To Model problems (AutoMapping)
body: <p><strong>Hi,</strong></p>
  
  <p>I am building a ASP.NET MVC application and have some problem with the data handling.</p>
  
  <p>One of my actions looks like this : </p>
  
  <pre><code>public ActionResult Register(int categoryId, int? filterId)
  </code></pre>
  
  <p>This action will instansiate the Registration form and in this case that means to load a custom ViewModel object with data from DB(<strong>Entity Framework</strong>) and send it to the strongly typed view.</p>
  
  <p>To translate the module object to the view object I am using <a href="http://automapper.codeplex.com/" rel="nofollow">AutoMapper</a></p>
  
  <p>When the user have modified the data and hit submit the followin action will be triggered :</p>
  
  <pre><code>[CustomAuthorize(Roles = SiteRoles.Admin)]
          [AcceptVerbs(HttpVerbs.Post)]
          public ActionResult Register(ModelViewRegisterFilter filter)
  </code></pre>
  
  <p>This action will run the followin code : </p>
  
  <pre><code>        if(filter.EditingFilter.Id &gt; 0)
              adCategoryFilter = Mapper.Map&lt;RegisterFilterItem, AdCategoryFilter&gt;(filter.EditingFilter, filterModel.GetFilter(filter.EditingFilter.Id));
          else
              adCategoryFilter = Mapper.Map&lt;RegisterFilterItem, AdCategoryFilter&gt;(filter.EditingFilter);
  </code></pre>
  
  <p>The adCategoryFilter is a class from Model(db). When this is done there should not be any problem to save the entity model object (adCategoryFilter).</p>
  
  <p><strong>Problem 1</strong>
  I have a field in the db table caled DisplayCheckBox and is of type nullable tinyint. To represent this I have created a nullable bool in my view object. When using the AutoMapper to translate from the Model to the ModelView I get the following exception : </p>
  
  <blockquote>
    <p>Trying to map System.Byte to
    System.Nullable`1[[System.Boolean,
    mscorlib, Version=4.0.0.0,
    Culture=neutral,
    PublicKeyToken=b77a5c561934e089]].
    Using mapping configuration for
    Biss.Models.Model.AdCategoryFilter to
    Biss.Views.ViewClasses.RegisterFilterItem
    Destination property: DisplayCheckBox
    Exception of type
    'AutoMapper.AutoMapperMappingException'
    was thrown.</p>
  </blockquote>
  
  <p>I have cheked the generated property of the model class and it look like this : </p>
  
  <pre><code>Nullable&lt;global::System.Byte&gt;
  </code></pre>
  
  <p><strong>Problem 2</strong>
  When the form is submitted the viewmodel object will be filled with data from the form. If there is a textBox that has the value "" (string.empty) then the string will be set to null.</p>
  
  <p>The db field that represent this textbox is of the type nchar(100) and this is generated by the entity framework to a string. The problem is that automapper will try to set the null value to the DisplayTextBox property on the model object and that will throw the followin exception :</p>
  
  <blockquote>
    <p>This property cannot be set to a null value.</p>
  </blockquote>
  
  <p><strong>Pleas advice.</strong></p>