Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9098325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:20:14+00:00 2026-06-17T00:20:14+00:00

I have to pass literal string to the Model from the view. Model has

  • 0

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

  1. Escape with double quote. Example -> “”key””
  2. Escape with Forward slash . Example -> \”key\”
  3. Without quotes. Example -> key
  4. Created const in model -> Example. Model.Key (Error -> instance is required)
  5. 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

  1. Some escape code in html
  2. Pass Enum value in html (like Category.Key)
  3. Pass const value in html (like Constants.Key)
  4. 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T00:20:15+00:00Added an answer on June 17, 2026 at 12:20 am

    How about creating a variable to hold the string in a Razor code block and passing it in to the dictionary?

    @{
        //Set the value of the key to a temporary variable
        var theKey = "key"; 
     }
    
    <!-- Reference the temporary variable in the indexer -->
    <a href="@Url.Content("~/temp/Data/" + Model.Dict[theKey])"></a>
    

    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

    public const string Foo = "Bar";
    

    or

    public static readonly Foo = "Bar";
    

    in

    public class ThePageModel
    {
        ...
    }
    

    Your code in the view would look more like

    <a href="@Url.Content("~/temp/Data/" + Model.Dict[MyApplication1.Models.ThePageModel.Foo])"></a>
    

    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.

    public enum MyEnum
    {
        MyDictionaryKey1,
        MyDictionaryKey2,
        MyDictionaryKey3
    }
    
    ...
    
    <a href="@Url.Content("~/temp/Data/" + Model.Dict[MyApplication1.Models.MyEnum.MyDictionaryKey1.ToString()])"></a>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to pass back information from a different source and can have a
I have to pass information from a desktop application to Web application and vice
I have a character arraym char Input[200]; input as of now has the string
My problem is as follows: I have a string literal that is macro-ed like
I have the following string read from an XML elememnt, and it is assigned
I have Django model and in one of the fields I need to store
I pass an object literal to a function like this: pre( {model:'MUserTry'} ); I
I have the following example code which uses a string literal as a template
You have to pass the args variable to the anonymous function, but the anonymous
I have to pass an object to an button's action... something like [myButton addTarget:self

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.