asp.net mvc - mv5 html.dropdownlist option elements have no id but have to be distinct -



asp.net mvc - mv5 html.dropdownlist option elements have no id but have to be distinct -

i retrieving info database using html dropdownlist helper. table contains repeating info need utilize distinct functionality along giving each alternative element unique value should value of alternative element itself.

here's code have developed far. distinct alternative elements have no id.

<div class="row"> <div class="col-md-3"> @html.dropdownlist("residentialbuilding", new selectlist(model.select(x => x.type).distinct())) </div> <div class="col-md-3"> @html.dropdownlist("residentialbuilding", new selectlist(model.select(x => x.stories).distinct())) </div> <div class="col-md-3"> @html.dropdownlist("residentialbuilding", new selectlist(model.select(x => x.size))) </div> <div class="col-md-offset-2 col-md-1"> <button type="button" class="btn btn-default"> <span class="glyphicon glyphicon-info-sign"></span> </button> </div>

update

here's code model:

namespace birdsresidential.models { using system; using system.collections.generic; public partial class residentialbuilding { public int id { get; set; } public string type { get; set; } public short stories { get; set; } public int size { get; set; } public string age { get; set; } public string orientation { get; set; } public string shape { get; set; } public int floorht { get; set; } public string foundation { get; set; } public int windowpercent { get; set; } public string heating { get; set; } public string cooling { get; set; } } }

here's code controller:

using system; using system.collections.generic; using system.data; using system.data.entity; using system.linq; using system.net; using system.web; using system.web.mvc; using birdsresidential.models; namespace birdsresidential.controllers { public class residentialbuildingcontroller : controller { private birdsreslocalentities db = new birdsreslocalentities(); // get: residentialbuilding public actionresult index() { //viewbag.residentialbuildings = new selectlist(db.residentialbuildings, "id", "type"); homecoming view(db.residentialbuildings.tolist()); }

firstly, bad practice have info access code in view layer. move appropriate business logic layer.

however, issue, problem aren't setting datavaluefield , datatextfield. need utilize different overloaded selectlist constructor. reference here. guessing sample code should this:

update:

there few issues here. regards getting distinct records, need implement icomparable. reason implement want select set of objects , "id" , "description". using distinct on set of objects 1 property not allow populate drop downwards boxes correctly.

in updated answer, have not done , updated code select boxes populated.

updated (controller):

using system; using system.collections.generic; using system.data; using system.data.entity; using system.linq; using system.net; using system.web; using system.web.mvc; using birdsresidential.models; namespace birdsresidential.controllers { public class residentialbuildingcontroller : controller { private birdsreslocalentities db = new birdsreslocalentities(); // get: residentialbuilding public actionresult index() { viewbag.residentialbuildings = new selectlist(db.residentialbuildings, "id", "type").distinct(); homecoming view(db.residentialbuildings.tolist()); } }

updated (view):

<div class="row"> <div class="col-md-3"> @html.dropdownlist("residentialbuilding", (viewbag.residentialbuildings selectlist)) </div> <div class="col-md-3"> @html.dropdownlist("residentialbuilding",(viewbag.residentialbuildings selectlist)) </div> <div class="col-md-3"> @html.dropdownlist("residentialbuilding", (viewbag.residentialbuildings selectlist)) </div> <div class="col-md-offset-2 col-md-1"> <button type="button" class="btn btn-default"> <span class="glyphicon glyphicon-info-sign"></span> </button> </div>

asp.net-mvc razor html-select

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -