开发者

Does the Razor View Engine in ASP.NET support structs or custom classes?

开发者 https://www.devze.com 2023-03-18 23:44 出处:网络
It appears that the Razor view engine supports Dictionary<>, List<>, and other collections.Does Razor support structs or cus开发者_StackOverflow社区tom classes such as below?If so what is

It appears that the Razor view engine supports Dictionary<>, List<>, and other collections. Does Razor support structs or cus开发者_StackOverflow社区tom classes such as below? If so what is the approach to use them?

struct gc {
    public string code;
    public string definition;
};

public class gc2 {
    public string code;
    public string definition;
};

The statements above are throwing compilation errors. (i.e. Compiler Error Message: CS1513: } expected)


I tried a work around.

By declaring an empty JSON object string - you can decode the string in Razor. By default, this creates the object which can then be used.

It looks kind of like this:

var word = "{\"code\" : [],\"font\" : []}";
var p = Json.Decode(word);

Now, p somewhat has the structure of the object needed.

However - if you try to fill the object - it becomes outside the bounds of the array.

0

精彩评论

暂无评论...
验证码 换一张
取 消