Dashless GUID not used DropDownListFor

这一生的挚爱 提交于 2019-12-24 13:50:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!