ASP.Net Html.DropDownList未选择所选元素

Kin*_*lan 13 asp.net-mvc html-select drop-down-menu

我有一个使用ASP.Net MVC Beta 5的网站,我刚刚将其升级到ASP.Net MVC 1.0.我在下拉列表中遇到了所选项目的问题.

跟随者有一个类似的问题(ASP.NET MVC RC中的Html.DropDownList(刷新)没有预先选择项目)但我没有答案(除了它可能是一个bug)

我的Controller方法如下所示:

[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);
 }
Run Code Online (Sandbox Code Playgroud)

该视图是针对模型的强类型,如下所示

public partial class View : ViewPage<IntegrationContainer> {}
Run Code Online (Sandbox Code Playgroud)

视图模板中的问题区域是:

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

我正在创建一个下拉列表,所选项目实际上从未被选中 - 这就是问题所在."item.CompanyID"是Guid,"id"是Guid,"name"是在IList中提供的公司对象上的字符串,该字符串保存在ViewData.Model.Companies实例中.

这实际上是一个错误吗? - 我发现很难理解为什么它仍然存在于ASP.Net MVC中...如果我做了什么,我会非常高兴.

无论如何,建议的工作是什么?

谢谢

Kin*_*lan 21

事实证明,如果通过Html.DropDownList控件的名称与集合对象的名称相同,则会导致ASP.Net MVC出现问题.

所以,如果我更改以下代码:

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

至:

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

一切正常.这是因为模型上的集合名称是Model.Companies .... bonkers ...还要注意,将控件名称从"公司"改为"公司"的情况也不起作用(这使得感觉我想).

我可以更改模型,但由于大多数是使用Linq-to-SQL构建的,我认为更改Html元素的名称更容易.

  • 谢谢,这仍然是Mvc3中的一个问题 (2认同)
  • 当它们发布到您的服务器时,您做了什么来使模型返回模型?我尝试了我的应用程序的建议,但后来MVC无法将值绑定回模型.你是从FormCollection得到的吗? (2认同)
  • 这仍然是MVC 4中的一个问题!:( (2认同)