I have to pass literal string to the Model from the view.
Model has a Dictionary<string,string> and i need to pass key from the view.
<a href="@Url.Content("~/temp/Data/" + Model.Dict[<Need to pass key here ???>])"
I have tried following but could not succeed
- Escape with double quote. Example -> “”key””
- Escape with Forward slash . Example -> \”key\”
- Without quotes. Example -> key
- Created const in model -> Example. Model.Key (Error -> instance is required)
- Escape with " -> Still some error
Following has worked, but looks ugly
1. Created readonly (not static) in Model.
I am looking for one of the following solutions
- Some escape code in html
- Pass Enum value in html (like Category.Key)
- Pass const value in html (like Constants.Key)
- Pass static value in html (like Model.Key)
Any one is fine, but specifying multiple/all in answer is welcomed.
Previously, there was array in place of dictionary, and passing index was working perfect.
<a href="@Url.Content("~/temp/Data/" + Model.Dict[0])"
I am a newbie to MVC. The question may be basic but I have given up.
How about creating a variable to hold the string in a Razor code block and passing it in to the dictionary?
To use a const (or any static from your model), you’ll have to use the type qualified name of the field/property, just like in code.
So if you’ve got
or
in
Your code in the view would look more like
Same goes for enums, though since your dictionary accepts a string and not whatever the enum type is, to make this example hang together there’ll be a
.ToString()after accessing the enum in the view.