简体   繁体   English

从MVC视图传递文字字符串到视图模型

[英]Passing literal string from MVC view to view model

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. 模型有一个Dictionary<string,string> ,我需要从视图传递密钥。

  <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"" 示例->“” key“”
  2. Escape with Forward slash . 以正斜杠转义。 Example -> \\"key\\" 示例-> \\“ key \\”
  3. Without quotes. 没有引号。 Example -> key 示例->键
  4. Created const in model -> Example. 在模型中创建了const->示例。 Model.Key (Error -> instance is required) Model.Key(错误->实例为必需)
  5. Escape with " -> Still some error 使用“->逃逸

Following has worked, but looks ugly 跟踪已奏效,但看起来很丑
1. Created readonly (not static) in Model. 1.在模型中创建只读(非静态)。

I am looking for one of the following solutions 我正在寻找以下解决方案之一

  1. Some escape code in html html中的一些转义代码
  2. Pass Enum value in html (like Category.Key) 在html中传递Enum值(如Category.Key)
  3. Pass const value in html (like Constants.Key) 在html中传递const值(如Constants.Key)
  4. Pass static value in html (like Model.Key) 在html中传递静态值(例如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. 我是MVC的新手。 The question may be basic but I have given up. 这个问题可能是基本的,但我已经放弃了。

You don't need to do anything fancy here; 您不需要在这里做任何花哨的事; the Razor view engine knows how to handle strings. Razor视图引擎知道如何处理字符串。

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

How about creating a variable to hold the string in a Razor code block and passing it in to the dictionary? 如何创建一个变量以将字符串保存在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>

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. 要使用const(或模型中的任何静态变量),必须像代码中那样使用字段/属性的类型限定名称。

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. 枚举也是如此,尽管由于您的词典接受的是字符串而不是枚举类型,所以为了使此示例悬挂在一起,在视图中访问枚举后将有一个.ToString()

public enum MyEnum
{
    MyDictionaryKey1,
    MyDictionaryKey2,
    MyDictionaryKey3
}

...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM