问题
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?
回答1:
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)
.
来源:https://stackoverflow.com/questions/33014645/dashless-guid-not-used-dropdownlistfor