0
votes

Hi,

I am building a ASP.NET MVC application and have some problem with the data handling.

One of my actions looks like this :

public ActionResult Register(int categoryId, int? filterId)

This action will instansiate the Registration form and in this case that means to load a custom ViewModel object with data from DB(Entity Framework) and send it to the strongly typed view.

To translate the module object to the view object I am using AutoMapper

When the user have modified the data and hit submit the followin action will be triggered :

[CustomAuthorize(Roles = SiteRoles.Admin)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Register(ModelViewRegisterFilter filter)

This action will run the followin code :

        if(filter.EditingFilter.Id > 0)
            adCategoryFilter = Mapper.Map<RegisterFilterItem, AdCategoryFilter>(filter.EditingFilter, filterModel.GetFilter(filter.EditingFilter.Id));
        else
            adCategoryFilter = Mapper.Map<RegisterFilterItem, AdCategoryFilter>(filter.EditingFilter);

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).

Problem 1 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 :

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.

I have cheked the generated property of the model class and it look like this :

Nullable<global::System.Byte>

Problem 2 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.

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 :

This property cannot be set to a null value.

Pleas advice.

1

1 Answers

1
votes

1) Automapper does not do any implicit conversions for you. You'll have to specify how to convert that value using one of automappers extensibility points.

Replying to the comment. This test will throw an exception:

   [TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        Mapper.CreateMap<HasByte, HasBool>();

        var hasByte = new HasByte() { Value = 1 };

        var hasBool = Mapper.Map<HasByte, HasBool>(hasByte);
    }
}

public class HasByte
{
    public Nullable<Byte> Value { get; set; }
}

public class HasBool
{
    public Nullable<bool> Value { get; set; }
}

2) Don't set a property that can't be null to null. Use the [Bind] attribute to ignore that property or check the posted form value for null before trying to Model bind it. Here is a decent article about using the Bind attribute:

http://ittecture.wordpress.com/2009/05/01/tip-of-the-day-199-asp-net-mvc-defining-model-binding-explicitly/