C# 传递字典以在HTML/Razor中查看和使用它

C# 传递字典以在HTML/Razor中查看和使用它,c#,asp.net-mvc,dictionary,razor,C#,Asp.net Mvc,Dictionary,Razor,这是一个模型: namespace WebApplication4.Models { public class myExampleModel { Dictionary<int, string> dict = new Dictionary<int, string>(); public Dictionary<int, string> returnDict () { dict

这是一个模型:

namespace WebApplication4.Models
{
    public class myExampleModel
    {
        Dictionary<int, string> dict = new Dictionary<int, string>();

        public Dictionary<int, string> returnDict ()
        {

            dict.Add(0,"a");
            dict.Add(1,"b");
            return dict;
        }

    }
}
和视图:

@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>meth2</title>
</head>
<body>
    <div> 
        <span>@ViewData["dict"]</span>
    </div>
</body>
</html>
@{
布局=空;
}
meth2
@ViewData[“dict”]
这是可行的,我在html
2
中看到了这一点

但我需要将整个
字典
从控制器传递到视图,因此,当我在控制器中尝试时:

namespace WebApplication4.Controllers
{
    public class myExampleController : Controller
    {
        public ActionResult meth2()
        {

            myExampleModel myExampleMod = new myExampleModel();

            ViewData["dict"] = myExampleMod.returnDict().Count;
            return View(ViewData);
        }
    }
}
public class myExampleController : Controller
{
    public ActionResult YourAction()
    {
        return View(new YourModelExample());
    }
}
ViewData[“dict”]=myExampleMod.returnDict();
返回视图(ViewData)

在HTML/Razor中,我尝试计数
字典
元素:

@ViewData[“dict”]计数

我得到一个错误:

“对象”不包含“计数”的定义…


问题:我在这里错过了什么?

ViewData
是属于
对象的。在访问它的属性之前,请尝试将其强制转换为
字典

<span>@((ViewData["dict"] as Dictionary<int, string>).Count)</span>
@((查看数据[“dict”]作为字典)。计数)

视图数据
属于
对象
。在访问它的属性之前,请尝试将其强制转换为
字典

<span>@((ViewData["dict"] as Dictionary<int, string>).Count)</span>
@((查看数据[“dict”]作为字典)。计数)

您需要将
ViewData[“dict”]
转换为
Dictionary

通常我会在页面顶部创建一个变量

@{
    Layout = null;
    var Dictionary = (Dictionary<int, string>)ViewData["dict"];
}

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>meth2</title>
</head>
<body>
    <div> 
        <span>@Dictionary.Count</span>
    </div>
</body>
</html>
@{
布局=空;
var Dictionary=(Dictionary)ViewData[“dict”];
}
meth2
@字典,伯爵

您需要将
ViewData[“dict”]
转换为
Dictionary

通常我会在页面顶部创建一个变量

@{
    Layout = null;
    var Dictionary = (Dictionary<int, string>)ViewData["dict"];
}

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>meth2</title>
</head>
<body>
    <div> 
        <span>@Dictionary.Count</span>
    </div>
</body>
</html>
@{
布局=空;
var Dictionary=(Dictionary)ViewData[“dict”];
}
meth2
@字典,伯爵

尝试使用强类型模型(STM)

控制器:

namespace WebApplication4.Controllers
{
    public class myExampleController : Controller
    {
        public ActionResult meth2()
        {

            myExampleModel myExampleMod = new myExampleModel();

            ViewData["dict"] = myExampleMod.returnDict().Count;
            return View(ViewData);
        }
    }
}
public class myExampleController : Controller
{
    public ActionResult YourAction()
    {
        return View(new YourModelExample());
    }
}
鉴于:

@model Dictionary<int, string>


<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>meth2</title>
</head>
<body>
    <div> 
        <span>@Model.Count</span>
    </div>
</body>
</html>
@model字典
meth2
@模型。计数

尝试使用强类型模型(STM)

控制器:

namespace WebApplication4.Controllers
{
    public class myExampleController : Controller
    {
        public ActionResult meth2()
        {

            myExampleModel myExampleMod = new myExampleModel();

            ViewData["dict"] = myExampleMod.returnDict().Count;
            return View(ViewData);
        }
    }
}
public class myExampleController : Controller
{
    public ActionResult YourAction()
    {
        return View(new YourModelExample());
    }
}
鉴于:

@model Dictionary<int, string>


<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>meth2</title>
</head>
<body>
    <div> 
        <span>@Model.Count</span>
    </div>
</body>
</html>
@model字典
meth2
@模型。计数

为什么不在视图中使用强类型模型?@Moo--@model WebApplication4.Models.myExampleModel--我在视图文件中添加了此代码。在这种情况下,语法是否正确?您缺少对
字典的强制转换
为什么不在视图中使用强类型模型?@Moo--@model WebApplication4.Models.myExampleModel--我将此代码添加到视图文件中。这种情况下的语法正确吗?您缺少对
字典的转换