1

我必须通过与 Image 一起显示键。但在我的示例中,我只能显示图像。如何使用包含密钥的文本框显示图像

控制器:

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();
        }
    }
}

剃刀视图:

@using (Html.BeginForm())
{                                                      
    foreach (var m in (List<string>)ViewData["list"])
    { 
        <ul>
            <li>       
                <img src="@m"  alt=""/>
            </li>
        </ul>
    } 
}

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

4

1 回答 1

2

我使用以下方法:

创建视图模型:

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>
    }
}
于 2013-08-11T12:21:56.533 回答