Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net Html.DropDownList Selected Element not Selected

I have a site that was using ASP.Net MVC Beta 5, and I have just upgraded it to ASP.Net MVC 1.0. I am having trouble with the selected item in a drop down list.

The follow person has a similar question (Html.DropDownList in ASP.NET MVC RC (refresh) not pre-selecting item) but I there is no answer (other than it might be a bug)

My Controller method looks as follows:

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult View(Guid id)
{
    IntegrationLogic logic = new IntegrationLogic(new IntegrationLinq());
    CompanyLogic companyLogic = new CompanyLogic(new CompanyLinq());
    IntegrationContainer container = new IntegrationContainer();

    container.Sources = logic.GetImportSource(id);
    container.Companies = companyLogic.GetCompanies(); // Returns a IList<company>
    container.SourceActions = logic.GetAllSourceActions(); // Returns an IList<SourceAction>
    container.SinkActions = logic.GetAllSinkActions();
    container.SuccessActions = logic.GetAllSuccessActions();
    container.FailureActions = logic.GetAllFailureActions();
    container.Actions = logic.GetAllActions();
    container.Watchers = logic.GetAllWatcherActions();
    container.ChainActions = logic.GetAllChainActions();

    return View("View", container);
 }

The view is a strongly typed against the Model as follows

public partial class View : ViewPage<IntegrationContainer> {}

The problem area in the view template is :

  <label for="Companies">Company: </label><%=Html.DropDownList("Companies",
                                                new SelectList(ViewData.Model.Companies, "id", "name", item.CompanyID))%>

I am creating a Dropdown List, the selected item never actually gets selected - and that is the problem. "item.CompanyID" is a Guid, "id" is a Guid and "name" is a string on the company object supplied in the IList that is held in the ViewData.Model.Companies instance.

Is this actually a bug ?- I find it hard to understand why this is still present in ASP.Net MVC... I would be totally happy if it is something I have done.

Regardless, what would be the suggested work around?

Thanks

like image 607
Kinlan Avatar asked Jul 30 '09 12:07

Kinlan


1 Answers

It turns out that if the name of your control via Html.DropDownList is the same name as the collection object it causes an issue with ASP.Net MVC.

So if I change the following code:

<label for="Companies">Company: </label><%=Html.DropDownList("Companies",
                                                new SelectList(ViewData.Model.Companies, "id", "name", item.CompanyID))%>

to:

<label for="Companies">Company: </label><%=Html.DropDownList("company",
                                                new SelectList(ViewData.Model.Companies, "id", "name", item.CompanyID))%>

all now works. This is because the name of collection on the model was Model.Companies.... bonkers... also note, that changing the case of the name of the control from "Companies" to "companies" does not work either (which makes sense I suppose).

I could change the Model, but as most of it is built using Linq-to-SQL I think it is easier to change the names of the Html elements.

like image 174
Kinlan Avatar answered Oct 21 '22 17:10

Kinlan