How to properly implement a drop-down box using <SelectListItem> and repository pattern in ASP.NET MVC -
i know how implement drop-down box using <selectlistitem>
type in asp.net mvc. have not used method before, , i've been advised best way go if have perform 'required' validation on drop-down.
i've created drop-down using viewbag, , don't think find approach effective.
my example here simple. small app allows user enter customer name , choose country drop-down. should check user selects country , enters value in name textbox. please have @ code below. incomplete , template, please me how implement approach. please feel free use repository if using effective. i'm still struggling understand how use repository pattern, , explanation needed on well. thanks
customer view model
public class customer { public int id { get; set; } [required] public string name { get; set; } [required] public country countryid { get; set; } } public class country { public int countryid { get; set; } public ienumerable<selectlistitem> countries { get; set; } }
model
- customers - entityframework entity containing table customer details holding foreign key countries entity
- countries - entity containing table countryid , countryname
action result
models.efentities ctx = new models.efentities(); public actionresult getcustomers() { using(ctx) { need code implement part } return view("customers"); } [httppost] public actionresult addcustomer(model.customer customer) { using(ctx) { //i'm thinking of calling savechanges() method on customers entity, //but please let me know if have better ways of writing code. // using repository pattern) } return view(); }
customers view view displays simple interface enter customer name , select country dorp-down. i'm thinking should 1 below
@model models.customer @html.editorfor(model=>model.name) @html.validationfor(model=>model.name) @html.dropdownlistfor(model => model.country..?..need code here well, "please select") @html.validationfor(...need code here make sure user selects country)
include select list in customer
public class customer { public int id { get; set; } [required] public string name { get; set; } [required] public int countryid { get; set; } public ienumerable<selectlistitem> countries { get; set; } }
build model in controller this:
public actionresult getcustomers() { var model = new customer(); using(ctx) { // need code implement part // > assume know how this, like: var customer = ctx.customers.get(the_id); // map entity model model.id = customer.id; // map rest of fields here (you may want use mapper tools) // country list db , map selectlistitem model.countries = mapcountries(ctx.countries.getall()); } return view(model); }
your view
@model models.customer @html.editorfor(model=>model.name) @html.validationfor(model=>model.name) // implement rest of fields @html.dropdownlistfor(model => model.countryid, model.countries, "please select") @html.validationfor(model => model.countryid)
then in post method
- make sure model valid (server-side validation)
- map model entity
- save entity database
that's it, need fill in blanks codes specific project.
Comments
Post a Comment