如何将字典从控制器传递到视图?

时间:2013-08-11 09:48:32

标签: asp.net-mvc-3 c#-4.0

我必须与Image一起传递给显示键。但是在我的例子中,我只能显示一个图像。我可以用包含键的文本框显示图像

 Controller: 
                        namespace SVGImageUpload.Controllers
                        {
                            public class TestController : Controller
                            {
                               Dictionary<string, string> _imgDict = null;//Dictionary datatype
                               public TestController()
                                {
                                    _imgDict = new Dictionary<string, string>();
                                    _imgDict.Add("Image1", "../../Images/wi0096-48.gif");
                                    _imgDict.Add("Image2", "../../Images/down.png");
                                    _imgDict.Add("Image3", "../../Images/wi0054-48.gif");
                                }

                        [HttpPost]
                                public ActionResult MapIcon(IEnumerable<string> image)
                                {
                                    Hidden hid = new Hidden();
                                    // hid.hiddevalue = image1;  
                                  //  string val = "";
                                    foreach (var item in image)
                                    {
                                        var keyValPair = item.Split('_');
                                        var key = keyValPair[0];
                                        var pair = keyValPair[1];

                                        var imgPath = _imgDict[pair];



                                        string val = imgPath;
                                        urls.Add(val);
                                        ViewData["list"] = urls; ;


                                    }
                                     return View("Custumize");
                        }

                            public ActionResult Custumize()
                            {
                                return View();
                            }
                        }
                    }






Razor View:       
            @using (Html.BeginForm())
            {                                                      
                foreach (var m in (List<string>)ViewData["list"])
                {



                <ul><li>

                        <img src="@m"  alt=""/>
                    </li>
                </ul>
                }




            }

我试过但没有成功。怎么做?

1 个答案:

答案 0 :(得分:2)

我使用以下方法:

创建ViewModel:

public class TestView
    {
       public Dictionary<string, string> dictionary { get; set; }
    }

比你的控制器:

   public ActionResult Something()
{
//**Your dictionary:
_imgDict = new Dictionary<string, string>();
 _imgDict.Add("Image1", "../../Images/wi0096-48.gif");
 _imgDict.Add("Image2", "../../Images/down.png");
 _imgDict.Add("Image3", "../../Images/wi0054-48.gif");

TestView model = new TestView
{
dictionary  = _imgDict,
};

return View(model
}

在视图中写下:

@using (Html.BeginForm())
            {                                                      
                foreach (var m in Model.dictionary)
                {

                <ul><li>

                        <img src="@m.Value"  alt="@m.Key"/>
                    </li>
                </ul>
                }

            }