asp.net mvc - Using Viewmodel to populate sql data and add columns -
i take model of table , make viewmodel additions fields, populate view. how do in controller? when model worked i'm not sure how same thing when it's viewmodel. i'm new asp.net mvc appreciated.
model:
public partial class tbltag { public int tagid { get; set; } public string tagname { get; set; } }
viewmodel:
public class tbltagviewmodel { public string tagname { get; set; } public string tagnameclr { { if (tagname == "test") { return "green"; } else { return "red"; } } } }
controller:
cannot implicitly convert type 'system.data.entity.dbset<_1mvcsqlserver.models.tbltag>' '_1mvcsqlserver.viewmodel.tbltagviewmodel'
now it's viewmodel structure entities different, assume problem. how resolve this?
private testentities db = new testentities(); public actionresult test() { viewmodel.tbltagviewmodel model = new viewmodel.tbltagviewmodel(); model = db.tbltags; return(model); }
here ended work. correct method?
public actionresult test() { list<viewmodel.tbltagviewmodel> list = new list<viewmodel.tbltagviewmodel>(); var model = new viewmodel.tbltagviewmodel(); foreach(tbltag p in db.tbltags) { viewmodel.tbltagviewmodel nw = new viewmodel.tbltagviewmodel(); nw.tagname = p.tagname; list.add(nw); } return view(list); }
correct method - create business layer, data db layer , pass data view layer (and vice versa, data view layer , pass db layer).
also, recommend @ ioc (di) approach :
business layer:
first @ all, create new classlibrary project , create business layer poco classes , abstraction of repository:
/// <summary> /// business layer class, similar on ef class /// </summary> public class tag { public int tagid { get; set; } public string tagname { get; set; } } public interface irepository { list<tag> gettaglist(); }
then create service:
public class service { private readonly irepository _repository; public service(irepository repository) { if (repository == null) throw new argumentnullexception("repository"); _repository = repository; } public list<tag> gettaglist() { return _repository.gettaglist(); } }
on stage have compiled version of business layer. can add complex logic of application , test it. attent, without db amd view parts!
next step - create db layer
db layer:
create 1 more class library in solution, add link bl project , add code this:
public class efrepository : irepository { private ... _db; public efrepository() { ..... } public list<tag> gettaglist() { var tags = (from in _db.tbltags select new tag { tagid = i.tagid, tagname = i.tagname }).tolist(); } }
so, have db , bl layers. , bl layer not depend on db layer, vice versa, db layer depend on bl layer .and it's correct approach, business logic of application should not depended on db!
the last step - view layer.
view layer
you can separate view layer different project too. inside asp.net mvc application. add references db , bl projects
public class tbltagviewmodel { public string tagname { get; set; } public string tagnameclr { { if (tagname == "test") { return "green"; } else { return "red"; } } } }
and controller:
public class homecontroller : controller { private readonly service _service; public homecontroller(irepository repository) { if (repository == null) throw new argumentnullexception("repository"); _service = new service(repository); } public actionresult test() { list<viewmodel.tbltagviewmodel> list = new list<viewmodel.tbltagviewmodel>(); vat taglist = _service.gettaglist(); foreach (tbltag p in taglist) { viewmodel.tbltagviewmodel nw = new viewmodel.tbltagviewmodel(); nw.tagname = p.tagname; list.add(nw); } return view(list); } }
and, last step, set db implementation web app:
public class mycontrollerfactory : defaultcontrollerfactory { protected override icontroller getcontrollerinstance(requestcontext requestcontext, type controllertype) { var efrepository = new efrepository(); if (controllertype == typeof(homecontroller)) { return new homecontroller(efrepository ); } return base.getcontrollerinstance(requestcontext, controllertype); }
pay attention, web-application not depend on db layer, depends on bl layer (this logical too). 1 place set efrepository in web application - in controller factory.
with approach can change efrepository implementation of irepository, different db or fake db. can test part of application without unnecessary relationships. have strong , correct architecture, application depend on business logic layer , not depend on db layer etc. changes in db layer or view layer not affect on logic layer
for mapping similar poco classes (i.e bl , db, bl , view classes) many properties can user automapper
Comments
Post a Comment