Model and BLL layer model conversion in MVC

Source: Internet
Author: User

1. there are two models: the congestion model and the blood loss model. The general practice is that the model is the model, and there is no way to operate it. It only has attributes. This model is called the blood loss model (which may be inaccurate ); A simple operation method is provided for a model, not just for a model with properties called a congestion model, as shown below:

Using system; using system. collections. generic; using system. LINQ; using system. web; namespace mvcapplication1.models {/// <summary> // a collection of presentation layer entities /// </Summary> public class usercompanyviewmodels {public userviewmodel user {set; get ;} public list <companyviewmodel> companys {set; get;} public usercompanyviewmodels () {user = new userviewmodel (); companys = new list <companyviewmodel> () ;}/// <summar Y> // Bo to VO /// </Summary> /// <Param name = "user"> business layer entity object </param> /// <Param name = "companys"> business layer entity object set </param> // <returns> presentation layer entity </returns> Public usercompanyviewmodels Tovo (usermodel user, list <companymodel> companys) {usercompanyviewmodels 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) {companyviewmodel Company = new companyviewmodel (); Company. companyid = item. companyid; Company. companyName = item. companyName; models. companys. add (company) ;}} Return Models ;}} /// <summary> /// representation layer entity /// </Summary> public class userviewmodel {public int userid {set; get;} Public String username {set; get ;}/// <summary> /// Presentation Layer entity // </Summary> public class companyviewmodel {public int companyid {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 object </param> // <returns> business layer object </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 companyid {set; get ;} public String companyName {set; get;} Public String companyaddress {set; get ;} /// <summary> // VO to Bo /// </Summary> /// <Param name = "models"> presentation layer object </param> // /<returns> business layer entity </returns> Publ IC 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 contains the model and BLL layer model conversion methods in MVC. Some people may ask why. What I want to say is that if this model conversion method is added, when different actions of the same controller need to be used, you can easily convert the page view model and the logic layer model without repeated writing.Code, Simple encapsulation.

2. Use in controller as follows:

[Httppost] public actionresult create (formcollection collection, usercompanyviewmodels models) {// if the control has an ID, you can use the names attribute of the control to set the value, for example, 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 execute the Database User table Operation Save, update, and other methods companymodel Company = new companymodel (); List <companymodel> companys = company. tobo (models); // then call the methods of The bll-layer database company Table Operation Save, update, and so on return view ("CREATE", models );} catch {return view ("CREATE ");}}

Here, we can see that the view may use a complex model. This model is a combination of several models at the business layer, or even a collection list type. Therefore, it can be easily converted here.

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.