Sunday 15 January 2012

c# - inteface issue in MVC 5 'Cannot create an instance of an interface' -



c# - inteface issue in MVC 5 'Cannot create an instance of an interface' -

hi i'm trying utilize interfaces in mvc 5 project code below:

public actionresult index(iaccountcontroller accountinterface) { var dynamicid_ddl = accountinterface.idmethod(); var model = new loggedinviewmodel { bidlistitems = new selectlist(dynamicid_ddl) }; viewbag.dynamicid_ddl = new list<id>(dynamicid_ddl); homecoming view(model); }

&

interface

public interface iaccountcontroller { languagesetting[] languagesettingmethod(); id[] idmethod(); }

however error:

cannot create instance of interface.

why happening , how can prepare ?

when mvc controllers called via route, model binder attempts find null ctor of parameter , initialize object prior entering controller action method. interfaces cannot instantiated... alter parameter type class implements interface if not concerned tight coupling.

or here's illustration on custom model binding

public class homecustombinder : imodelbinder { public object bindmodel(controllercontext controllercontext, modelbindingcontext bindingcontext) { httprequestbase request = controllercontext.httpcontext.request; string title = request.form.get("title"); string day = request.form.get("day"); string month = request.form.get("month"); string year = request.form.get("year"); homecoming new homepagemodels { title = title, date = day + "/" + month + "/" + year }; } public class homepagemodels { public string title { get; set; } public string date { get; set; } } }

c# asp.net-mvc

No comments:

Post a Comment