There will be a comparison to receive goods, there is a need to find more.
In the traditional WEBFORMK development work, we often have the following code block
//Save protected voidbtnSubmit_Click (Objectsender, EventArgs e) { Try{BLL. MONEYBLL Cun=NewBLL. MONEYBLL (); Model.money M1=NewModel.money (); M1.commany=int. Parse ( This. Commany.text); M1. Countryid=int. Parse ( This. cou. Value); M1. Countryids=""; M1. MaxX=float. Parse ( This. Maxx.text); M1. MinX=float. Parse ( This. Minx.text); if(!string. IsNullOrEmpty ( This. Xprice.text)) {M1. Xprice=float. Parse ( This. Xprice.text); } if(!string. IsNullOrEmpty ( This. Yprice.text)) {M1. Yprice=float. Parse ( This. Yprice.text); } M1. Type=int. Parse ( This. Type.text); intres =Cun. ADD (M1); //jscriptmsg ("Add fee information successfully!") "," moneylist.aspx "," Success "," Parent.loadmenutree "); } Catch { //jscriptmsg ("Add fee information failed, please check the entry legitimacy!") "," moneylist.aspx "," Error "," Parent.loadmenutree "); } }
That is, when the Save button is clicked, the value one by one of the form element in the form form is obtained, converted to the corresponding target type, assigned to a new entity class object property, to implement the related function point development.
The above code blocks in our daily development work frequently appear, the similarity degree is high and high.
There is an excellent solution for the general purpose of this situation in MVC,--tryupdatemodel usage is as follows
New Funmodel (); TryUpdateModel (model, formcollection.allkeys);
which
Formcollection.allkeys is an optional parameter, if the parameter is not passed in, TryUpdateModel will get all key values in the context of the current request, including the key value in Request.QueryString (eg:a.aspx?a=1&b=2), And all key values in the Request.Form, including all the key-value pairs in the context of the entire request,
And then loop all of these, through the reflection mechanism to obtain the entity object model corresponding to the properties of the key, and if there is a property named key, then get the value of the key value of the packet to change key corresponding value, through the form of reflection to assign to the corresponding attribute of the entity class, Complete the assembly of the data within the packet to the entity object, and in this process there is a type conversion of the value in the packet to the entity object property type, omitting the tedious int. The Parse (This.Commany.Text) Judgment, transformation operation, greatly simplifies the appearance of similar code in functional development. Formcollection.allkeys ———————— All key values in the Request.Form.