1

I have to post image src attribute to view.So i have tried in the following way

Razor View:

@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>
    }

}

Controller:

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

My main problem is that i have to show those images which are selected. I have tried but not succedded. The value of img tag are obtained in controller. But how to send these values again back to view?

1
  • val is of type string. you put it into the ViewBag in the controller. but then, in view, you try to retrieve it as List<string>. that won't work. Commented Aug 9, 2013 at 10:27

3 Answers 3

1

Have you tried passing the data in your view with a model?

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

With in your view:

@model IEnumerable<string>

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

}
Sign up to request clarification or add additional context in comments.

Comments

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

On View

@foreach(var item in Model)
{
 <p> @item.url</p>   
}

1 Comment

Or You can cast your ViewData to List type and same foreach loop at View will work .But you have already defined a List item why pass it in ViewData when you can directly pass it to View .
-1

User $.ajax and jquery and onclick event on clicked img.

3 Comments

the values of clicked images are obtained in controller .My problem is how to send these values back to view so that i will render these selected images
$ Post does not give you a variable ViewBag or ViewData.
maybe session i use usually $ajax

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.