C# 将文本字符串从MVC视图传递到视图模型

C# 将文本字符串从MVC视图传递到视图模型,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,我必须从视图向模型传递文本字符串 public enum MyEnum { MyDictionaryKey1, MyDictionaryKey2, MyDictionaryKey3 } ... <a href="@Url.Content("~/temp/Data/" + Model.Dict[MyApplication1.Models.MyEnum.MyDictionaryKey1.ToString()])"></a> 模型有一个字典,我需要

我必须从视图向模型传递文本字符串

public enum MyEnum
{
    MyDictionaryKey1,
    MyDictionaryKey2,
    MyDictionaryKey3
}

...

<a href="@Url.Content("~/temp/Data/" + Model.Dict[MyApplication1.Models.MyEnum.MyDictionaryKey1.ToString()])"></a>
模型有一个
字典
,我需要从视图中传递密钥

  <a href="@Url.Content("~/temp/Data/" + Model.Dict[<Need to pass key here ???>])" 
“密钥”
  • 用正斜杠逃跑。示例->“密钥”
  • 没有引用。示例->键
  • 在模型->示例中创建常量。Model.Key(错误->需要实例)
  • 用“->转义仍然有一些错误
  • 下面的内容有效,但看起来很难看
    1.在模型中创建只读(非静态)

    我正在寻找以下解决方案之一

  • html中的一些转义代码
  • 在html中传递枚举值(如Category.Key)
  • 在html中传递常量值(如Constants.Key)
  • 在html中传递静态值(如Model.Key)
  • 任何一个都可以,但是在答案中指定多个/全部都是受欢迎的

    以前,用数组代替字典,传递索引工作得很好

    <a href="@Url.Content("~/temp/Data/" + Model.Dict[0])" 
    

    您不需要在这里做任何花哨的事情;Razor视图引擎知道如何处理字符串

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

    您不需要在这里做任何花哨的事情;Razor视图引擎知道如何处理字符串

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

    创建一个变量将字符串保存在Razor代码块中,并将其传递到字典中如何

    @{
        //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>
    

    视图中的代码看起来更像

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

    创建一个变量将字符串保存在Razor代码块中,并将其传递到字典中,怎么样

    @{
        //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>
    

    视图中的代码看起来更像

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

    错误CS1502:“System.Collections.Generic.Dictionary.this[string]的最佳重载方法匹配项”'有一些无效的参数,这意味着您没有将字符串传递到索引器,而是传递了其他内容。请粘贴生成该错误的确切代码。它起作用了。这是一条长线,需要在两个位置使用索引器。其他位置由于滚动而不可见。现在我在两个位置都应用了,并且都起作用了。我的粗心。您能演示如何读取传递静态字符串吗?例如Model.Key(其中Key是const)错误CS1502:与“System.Collections.Generic.Dictionary.this[string]匹配的最佳重载方法'有一些无效的参数,这意味着您没有将字符串传递到索引器,而是传递了其他内容。请粘贴生成该错误的确切代码。它起作用了。这是一条长线,需要在两个位置使用索引器。其他位置由于滚动而不可见。现在我在两个位置都应用了,并且都起作用了。我的粗心。您能演示如何读取pass静态字符串吗?比如Model.Key(其中Key是const)它起作用了。我的坏。修复需要多个位置,我只提供了一个位置。但我也在寻找其他方法。这看起来也不错。它起作用了。我的坏。修复需要多个位置,我只提供了一个位置。但我也在寻找其他方法。这看起来也不错。