简体   繁体   English

在asp.net mvc 2.0中使用Html.DropDownListFor helper时如何更改id值?

[英]How to change id value when using Html.DropDownListFor helper in asp.net mvc 2.0?

I have a partial view that has something like this 我有一个像这样的部分观点

 <%= Html.DropDownListFor(m => m.SelectedProductName, Model.ProductList, "Select a Product") %>

Now you can create a new product and edit an existing product. 现在,您可以创建新产品并编辑现有产品。 Both editing and creating use the same form. 编辑和创建都使用相同的表单。 The create is on the main page on load up. 加载时,创建位于主页面上。 Edit pops up in a jQuery UI model dialog and renders a new partial view. 编辑在jQuery UI模型对话框中弹出,并呈现新的局部视图。

So as far as the page is concerned I have 2 dropdown boxes with the same "id" which is bad since they should be unique. 因此,就页面而言,我有2个具有相同“id”的下拉框,这是不好的,因为它们应该是唯一的。 So how do I change the id? 那么我该如何更改ID呢? So when the edit loads it might have a id of "editSelectedProductName"? 因此,当编辑加载时,它的id可能是“editSelectedProductName”?

I tried to do this in the view model 我试图在视图模型中执行此操作

public string SelectedProductName{ get; set; }

ViewModelConstructor()
{
  SelectedProductName = "EditSelectedProductName";
}

But it seems to not care and keeps using "SelectedProductName" as the product name 但它似乎并不关心并继续使用“SelectedProductName”作为产品名称

I can't find the documentation at the moment, but there is an overload for DropDownListFor that will accept an object-typed collection of attributes (HtmlAttributes is the parameter name.) 我目前找不到文档,但DropDownListFor有一个重载,它将接受一个对象类型的属性集合(HtmlAttributes是参数名称。)

It will look something like this: 它看起来像这样:

Html.DropDownListFor(model=>model.SomeProperty, new {@id="UniqueID1234"});

You can use Intellisense to find the overload that includes HtmlAttributes. 您可以使用Intellisense查找包含HtmlAttributes的重载。

你可以使用硬编码的jQuery:

$('select#[id of your select box]').attr('id', '[id that you want]');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM