# Modifying the DataSourceRequest Object to Map ViewModel Objects to EF Core Entities and Support Filtering and Sorting

## Environment

<table>
	<tbody>
		<tr>
			<td>Product Version</td>
			<td>2023.1.117</td>
		</tr>
		<tr>
			<td>Product</td>
			<td>DataSource for ASP.NET MVC</td>
		</tr>
	</tbody>
</table>

## Description

I want to use Model Mapping, but my view model object and entities returned by Entity Framework are not identical. Thus, when I follow the [Model mapping documentation](https://docs.telerik.com/aspnet-core/html-helpers/datasource/overview#model-mapping) I get an exception.

I have an EF Core entity:

```C#
public class Product
{
 public long ID {get;set;}
 public string Product_Name {get;set;}
}
```

And a corresponding ViewModel:

```C#
public class ProductVM
{
  public long Id {get;set;}
  public string ProductName {get;set;}
}
```

## Solution

A possible approach to resolve the issue is to implement model mapping and map the different model properties to support filtering and sorting. The [solution](https://www.telerik.com/forums/todatasourceresult) below is suggested by Dave, a member of our forums, and shows how to map model properties so filtering and sorting can be used:

```C#
    public static class DataSourceRequestExtensions
    {
        public static void RemapMember(this DataSourceRequest request, string memberName, string newMemberName)
        {
            foreach (var sort in request.Sorts)
            {
                if (sort.Member.Equals(memberName))
                {
                    sort.Member = newMemberName;
                }
            }

            foreach (var filter in request.Filters)
            {
                if (filter is CompositeFilterDescriptor compositeFilterDescriptor)
                {
                    foreach (var compositeFilter in compositeFilterDescriptor.FilterDescriptors)
                    {
                        RemapFilterDescription(compositeFilter, memberName, newMemberName);
                    }
                }
                RemapFilterDescription(filter, memberName, newMemberName);
            }
        }

        private static void RemapFilterDescription(IFilterDescriptor filter, string memberName, string newMemberName)
        {
            if (!(filter is FilterDescriptor descriptor)) return;

            if (descriptor.Member.Equals(memberName))
            {
                descriptor.Member = newMemberName;
            }
        }
    }
```

Use the extension method to map the model properties so filtering and sorting works as expected:

```C#
        public ActionResult Grid_Read([DataSourceRequest] DataSourceRequest request)
        {
            var data = myProductsService.GetAllProducts();
            request.RemapMember("Id", "ID");
            request.RemapMember("ProductName", "Product_Name");
            var dsResult = data.ToDataSourceResult(request, model => new ProductVM()
            {
                Id = model.ID,
                ProductName = model.Product_Name
            });
            return Json(dsResult);
        }
```

## More ASP.NET MVC Grid and DataSource Resources

* [ASP.NET MVC DataSource Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/datasource/overview)

* [ASP.NET MVC DataSource Demos](https://demos.telerik.com/aspnet-mvc/datasource/index)

* [ASP.NET MVC Grid Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/data-management/grid/overview)

* [ASP.NET MVC Grid Demos](https://demos.telerik.com/aspnet-mvc/grid/index)

* [ASP.NET MVC Grid Product Page](https://www.telerik.com/aspnet-mvc/grid)

* [Telerik UI for ASP.NET MVC Video Onboarding Course (Free for trial users and license holders)](https://www.telerik.com/aspnet-mvc/documentation/getting-started/video-onboarding)

* [Telerik UI for ASP.NET MVC Forums](https://www.telerik.com/forums/aspnet-mvc)

## See Also

* [Server-Side API Reference of the Grid for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/grid)
* [Client-Side API Reference of the Grid for ASP.NET MVC](https://docs.telerik.com/kendo-ui/api/ui/grid)
* [Server-Side API Reference of the DataSource for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/datasource)
* [Telerik UI for ASP.NET MVC Breaking Changes](https://www.telerik.com/aspnet-mvc/documentation/backwards-compatibility/2023-backwards-compatibility)
* [Telerik UI for ASP.NET MVC Knowledge Base](https://docs.telerik.com/aspnet-mvc/knowledge-base)
