views:

240

answers:

1

Hi

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 a existing product. Both editing and creating use the same form. The create is on the main page on load up. Edit popus up in a jquery u.i model dialog and renders a new partial view.

So as far as the page is concerned is that I have 2 dropdown boxes with the same "id" which is bad since they should be unique. So how do I change the id? So when the edit loads it might have a id of "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.

Thanks

A: 

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.)

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.

Dave Swersky
I know it has the htmlAttributes property but in your example you hard-coded it. I can't hard-coded it since I am using the same partialview for both cases. Only way I can see is I have to make a special property in the view data model just to hold the id. I was hoping I would not have to do that.
chobo2
Another point to mention is the "name" attribute of that list will still be "SelectedProductName" even if you try to override it @name = "NotIdName" which kinda sucks but I don't think it should effect things( I at least hope not).
chobo2