Dashless GUID not used DropDownListFor
Asked Answered
S

1

1

I have a page, which accepts a parameter x (which is a GUID). On the page I have a

HTML.DropDownListFor(x => x.SomeGuidProperty).

If I specify parameter x in URL as 43f67d68-066d-4fda-94a8-3e3e4c7c278a (with dashes), drop down selects respeced option. But if I specify parameter x in URL as 43f67d68066d4fda94a83e3e4c7c278a (still GUID, but no dashes) - DropDownList ignores it.

It is impossible to ensure single format of GUID, as page is used by different sources.

What can be done to make DropDownList understand parameter x?

Scheld answered 8/10, 2015 at 11:36 Comment(6)
The 2 values are not the same - how would you expect then to match?Blackwell
Well, they both represent same GUID value. The model, passed to the view, has x as GUID property. And although it is passed as GUID (not as string) to DropDownList, it is completely ignored, if in URL x has no dashes (or has some other GUID format, which is not "D" format). Simple output of x, shows that it's value is really a GUID and is independant from x in URL.Scheld
how can you ensure the format a data posted as url? if it was a method parameter you could, but here you will need to check the presence of dashes and include when necessaryErumpent
@Ricardo I have method in controller, which accepts x as GUID, puts x's value to model, and returns a view with the model supplied. Inside of the view I have a drop down list <-- and it makes a problem.Scheld
I got. Maybe can you accept parameter as string and convert to a Guid? It will receive everything, and you can check for dashes before convert.Erumpent
It will not change anything because I already have it as GUID.Scheld
M
0

Instead of binding directly to a Guid, you'll need to bind to a string and then programmatically convert to a Guid. For example, instead of something like:

public ActionResult Foo(Guid x)

You'll need:

public Actionresult Foo(string x)

Then inside, you'll want to format x as a Guid should be formatted, if it's not already before converting it to an actual Guid: var guid = new Guid(x).

Merganser answered 8/10, 2015 at 13:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.