Featured post

c# - Usage of Server Side Controls in MVC Frame work -

i using asp.net 4.0 , mvc 2.0 web application. project requiremrnt have use server side control in application not possibl in noraml case. ideally want use adrotator control , datalist control. i saw few samples , references in codepleax mvc controllib howwver found less useful. can tell how utilize theese controls in asp.net application along mvc. note: please provide functionalities related adrotator , datalist controls not equivalent functionalities thanks in advace. mvc pages not use normal .net solution makes use of normal .net components impossible. a normal .net page use event driven solution call different methods service side mvc use actions , view completly different way handle things. also, mvc not use viewstate normal .net controlls require. found article discussing mixing of normal .net , mvc.

dynamic update the data using EF4 in asp.net mvc -


for example, have data table named [user], , mapping user enetity ef4:

id, fullname, emailaddress, fax, tel 

in web page, want update "emailaddess" , "fax" 2 properties below:

@using (html.beginform("edit", "user")) {     <label>email address:</label>     @html.textbox("emailaddress")     <label>fax:</label>     @html.textbox("fax")     <input type="submit" value="update" /> } 

and controller did this:

public actionresult edit(user user) {  var _user = getusers().where(a => a.id == user.id).singleordefault();  _user.fax = user.fax; _user.emailaddress = user.emailaddress;  context.savechanges(); } 

yes, updated , successful, next time want update "tel", need change code this:

_user.fax = user.fax; _user.emailaddress = user.emailaddress; _user.tel = user.tel; 

if have 100 fields, crazy doing :(

so, how can update models database related fields in web page?

thanks

you should automapper ( http://automapper.codeplex.com/ ).

automapper.mapper.createmap(typeof(user), typeof(userviewmodel));  automapper.mapper.createmap<user, userviewmodel>()     .formember(d => d.property1, f => f.mapfrom(s => s.property1))     .formember(d => d.property2, f => f.mapfrom(s => s.property2))     .formember(d => d.property3, f => f.mapfrom(s => s.property3)); 

then in edit can following

public actionresult edit(user user) {     var _user = getusers().where(a => a.id == user.id).singleordefault();      automapper.mapper.map(user, _user);      context.savechanges(); } 

entity framework update fields have changed.

pretty sure that's need.


Comments

Popular posts from this blog

c# - Usage of Server Side Controls in MVC Frame work -

cocoa - Nesting arrays into NSDictionary object (Objective-C) -

ios - Very simple iPhone App crashes on UILabel settext -