The model BLL models in MVC are transferred to each other

Source: Internet
Author: User

The model BLL models in MVC are transferred to each other The model can usually do 2 kinds: congestion model and blood loss model, the general practice is that the model is the model, there is no method to operate, only have attributes, this is called the Blood loss model (may not be accurate), with a simple operation of the model, not only the model has a property called the congestion model, as follows:
Using system;using system.collections.generic;using system.linq;using system.web;namespace MvcApplication1.Models{/ <summary>////Presentation entity collection///</summary> public class Usercompanyviewmodels {public Uservie        Wmodel User {set; get;}        Public list<companyviewmodel> companys {set; get;}            Public Usercompanyviewmodels () {User = new Userviewmodel ();        Companys = new list<companyviewmodel> (); }///<summary>//Bo to Vo///</summary>//<param name= "user" > Business Layer Entity Object &L t;/param>//<param name= "companys" > Business Layer Entity Objects Collection </param>//<returns> Presentation layer entities &LT;/RETURNS&G        T Public usercompanyviewmodels Tovo (usermodel user, list<companymodel> companys) {USERCOMPANYVIEWMO            Dels models = new Usercompanyviewmodels (); if (user! = null) {models. User.userid = user.       UserID;         Models. User.username = user.            UserName; } if (companys! = null && companys. Count > 0) {foreach (var item in companys) {COMPANYVIEWMO                    Del Company = new Companyviewmodel ();                    Company.companyid = Item.companyid;                    Company.companyname = Item.companyname;                MODELS.COMPANYS.ADD (company);        }} return models; }}///<summary>//Presentation entity///</summary> public class Userviewmodel {public int Us        Erid {set; get;}    public string UserName {set; get;} }///<summary>//Presentation entity///</summary> public class Companyviewmodel {public int Comp        Anyid {set; get;}    public string CompanyName {set; get;} }///<summary>//Business layer Entity///</summary> public class Usermodel {public int UserID {SET Get        } public string UserName {set; get;}        public string Usergander {set; get;}        public string UserAddress {set; get;} <summary>//Vo to Bo//</summary>/<param Name= "models" > Presentation layer Entity object </para            m>//<returns> Business layer Entities </returns> public Usermodel Tobo (Usercompanyviewmodels models) {            Usermodel user = new Usermodel (); if (Models! = Null&&models. User!=null) {User. UserID = models.                User.userid; User. UserName = models.            User.username;        } return user; }}///<summary>//Business layer Entity///</summary> public class Companymodel {public int Com        Panyid {set; get;}        public string CompanyName {set; get;}        public string CompanyAddress {set; get;} <summary>//Vo to Bo//</summary>//<param name= "Models" > Presentation layer Entity Objects </param>//<returns> Business layer entities </returns> public list<companymodel>             Tobo (Usercompanyviewmodels models) {list<companymodel> companys = new list<companymodel> (); if (Models! = Null&&models. Companys!=null && models. Companys.count > 0) {foreach (var item in models).                    Companys) {Companymodel company = new Companymodel (); Company. CompanyID = Item.                    CompanyID; Company. CompanyName = Item.                    CompanyName; Companys.                ADD (company);        }} return companys; }    }}

The model and the BLL layer models in MVC have been transformed in this mode, and there may be people who ask why, I would say that if the model conversion method is added, when the same controller has different action to use, You can easily convert the page view model and the logical layer model, without having to write the code repeatedly and simply encapsulate the idea.


Two. Use in controller, as follows:

                [HttpPost]        Public ActionResult Create (formcollection collection,usercompanyviewmodels models)        {            //If the control has an ID, You can use the control names property to take a value, such as            var a = collection["User.userid"];            Try            {                //todo:add insert logic here                usermodel user = new Usermodel ();                user = user. Tobo (models);                                Then call the BLL layer to perform the database user table operation Save,update method                  Companymodel company = new Companymodel ();                List<companymodel> companys = Company. Tobo (models);                Then call the BLL Layer Database company table operation Save,update and other methods                  return View ("Create", models);            }            Catch            {                return View ("Create");            }        }

As you can see here, because the view view is likely to use a complex model, the model is a combination of several model of the business layer, even the collection list type, so it can be easily converted here.

The model BLL models in MVC are transferred to each other

Related Article

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.