. Net MVC AutoMapper Simple to use

Source: Internet
Author: User

1. Install the AutoMapper nuget package.

2. Create a new AutoMapper configuration class and implement a static configuration method.

using AutoMapper; using Automappertest.models; namespace automappertest.app_start{    publicclass  automapperconfig    {          Public Static void Config ()        {            = =            {                cfg. Createmap<studententity, studentoutput>();            });     }}

3. Reference the configuration method in the global configuration global.asax.

using Automappertest.app_start; using SYSTEM.WEB.MVC; using System.Web.Optimization; using System.Web.Routing; namespace automappertest{    publicclass  MvcApplication:System.Web.HttpApplication    {        protectedvoid  Application_Start ()        {            Automapperconfig.config ();     }}}

4. Specific Use

         PublicJsonresult Getmapper () {//instantiating a list of entitiesList<studententity> studentlist =NewList<studententity>(); //Analog DataStudentlist.add (Newstudententity {Id=1, Age= A, Gander=" Boy", Name="wangzeling", Say="Only the paranoid survive", score=99M}); //Auotmapper How to convert list<studentoutput> to list<studentoutput> using specific methodslist<studentoutput> Output = automapper.mapper.map<list<studentoutput>>(studentlist); returnJson (Output, jsonrequestbehavior.allowget); }

Attached: entity class, Output class

     Public classstudententity { Public intId {Get;Set; }  Public stringName {Get;Set; }  Public intAge {Get;Set; }  Public stringGander {Get;Set; }  Public decimalScore {Get;Set; }  Public stringSay {Get;Set; } }
     Public class Studentoutput    {        publicstringgetset;}          Public decimal Get Set ; }          Public string Get Set ; }    }

Attached: AutoMapper GitHub

HTTPS://github.com/automapper/automapper

. Net MVC AutoMapper Simple to use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.