1

我必须将图像 src 属性发布到视图。所以我尝试了以下方式

剃刀视图:

@using (Html.BeginForm("MapIcon", "test", FormMethod.Get))
{
    <div class="selected" id="selectable_images">
        <table>
            <tr>
                <td>
                    <img src="../../Images/wi0096-48.gif" alt="Image1" class="conversation_img" />
                </td>
                <td>
                    <img src="../../Images/down.png" alt="image2" />
                </td>
                <td>
                    <img src="../../Images/wi0054-48.gif" alt="Image3" />
                </td>
                <td>
                    <img src="../../Images/Photo-icon.png" alt="image4" />
                </td>
            </tr>
            <tr>
                <td>
                    <img src="../../Images/wi0062-48.gif" alt="image5" />
                </td>
                <td>
                    <img src="../../Images/wi0064-48.gif" alt="image6" />
                </td>
                <td>
                    <img src="../../Images/wi0126-48.gif" alt="image7" />
                </td>
                <td>
                    <img src="../../Images/wi0126-48.gif" alt="image8" />
                </td>
            </tr>
            <tr>
                <td>
                    <img src="../../Images/wi0063-64.gif" alt="image9" />
                </td>
                <td>
                    <img src="../../Images/wi0064-48.gif" alt="image10" />
                </td>
                <td>
                    <img src="../../Images/wi0009-48.gif" alt="image11" />
                </td>
                <td>
                </td>
            </tr>
            <tr>
                <td>
                    <img src="../../Images/wi0126-48.gif" alt="iamg12" />
                </td>
                <td>
                    <img src="../../Images/wi0124-48.gif" alt="image13" />
                </td>
                <td>
                    <img src="../../Images/wi0062-48.gif" alt="image14" />
                </td>
                <td>
                </td>
            </tr>
        </table>
    </div>
}
@using (Html.BeginForm("MapIcon", "test", FormMethod.Post, new { id = "postform" }))
{

    <input id="image2" type="hidden" name="image" value="../../Images/down.png">
    <input id="Image3" type="hidden" name="image" value="../../Images/wi0054-48.gif">
    <input id="image4" type="hidden" name="image" value="../../Images/Photo-icon.png">

    <input type="submit" value="Match" />

    foreach (var m in (List<string>) ViewData["list"]) //I am getting Null value here 
    {
        <ul>
            <li>
                <img src="@m"  alt=""/>
            </li>
        </ul>
    }

}

控制器:

[HttpPost]
public ActionResult MapIcon(IEnumerable<string> image)
{
    List<string> urls = new List<string>();
    string val = "";
    foreach (var item in image)
    {
        val = item;
        urls.Add(val);
        //ViewBag.list = val;

        ViewData["list"] = urls; ;

    }
    return View();
}

我的主要问题是我必须显示那些被选中的图像。我试过但没有成功。img标签的值在控制器中获取。但是如何将这些值再次发送回查看?

4

3 回答 3

1
[HttpPost]
public ActionResult MapIcon(IEnumerable<string> image)
{
    List<string> urls = new List<string>();
    string val = "";
    foreach (var item in image)
    {
        val = item;
        urls.Add(val);
        //ViewBag.list = val;

        //ViewData["list"] = urls; ;

    }
        return View(urls);
}

在视图中

@foreach(var item in Model)
{
 <p> @item.url</p>   
}
于 2013-08-09T11:45:25.027 回答
1

您是否尝试过使用模型在视图中传递数据?

[HttpPost]
public ActionResult MapIcon(IEnumerable<string> image)
{
    return View(image);
}

在您看来:

@model IEnumerable<string>

using (Html.BeginForm())
{
    foreach (var m in Model)
    {
        <ul>
            <li>
                <img src="@m"  alt=""/>
            </li>
        </ul>
    }

}
于 2013-08-09T10:31:43.550 回答
-1

用户 $.ajax 和 jquery 以及单击的 img 上的 onclick 事件。

于 2013-08-09T10:14:54.383 回答