Dashless GUID not used DropDownListFor

2019-08-12 07:14发布

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条回答
我欲成王,谁敢阻挡
2楼-- · 2019-08-12 08:01

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

查看更多
登录 后发表回答