0

Existing code in a project is using Url.Action to obtain a fully qualified URL to display in a dialog. So it has a controller function that looks like:

public ActionResult CheckItem(bool isCorrect, string id){}

and then Url.Action is simply:

Url.Action("CheckItem", new { isCorrect =  true, id = 2})

Now this all works just fine. But I have to send a List<> of objects, I have this all working by submitting a form.

So my question is: Is there a way of submitting a form using Url.Action? If not what is the best way of submitting my form and getting the URL back.

Thanks.

TBD
  • 771
  • 1
  • 11
  • 27

1 Answers1

1

I didn't understand exactly what you need, but I think that you have some items selection from the user maybe using checkboxes or whatever.

The answer might be on this link: CheckboxList in MVC3.0

Basically what do you have to do is: create an Action that receives a List or a IEnumerable items and put your form to POST to that Action.

I made a sample code too that could help:

You could have an Item model:

using System;

namespace SandboxMvcApplication.Models
{
    public class Item
    {
        public int Id { get; set; }
        public string Title { get; set; }
    }
}

Your controller could be:

public class HomeController : Controller
{
    List<Item> itemList = new List<Item>() {
            new Item() { Id = 1, Title = "Item 1" },
            new Item() { Id = 2, Title = "Item 2" },
            new Item() { Id = 3, Title = "Item 3" }
        };

    public ActionResult Index()
    {
        return View(itemList);
    }

    public ActionResult ProcessForm(int[] items)
    {
        var selectedItems = new List<Item>();
        foreach (var item in items)
        {
            selectedItems.AddRange(itemList.Where(i => i.Id == item));
        }

        return View("Success", selectedItems);
    }
}

An Index View (~/Views/Home/Index.cshtml):

@model List<SandboxMvcApplication.Models.Item>

@{
    ViewBag.Title = "Home Page";
}

<form action="@Url.Action("ProcessForm")" method="post">
    <ul>
        @foreach (var item in Model)
        {
            <li><input type="checkbox" name="items" value="@item.Id" />@item.Title</li>
        }
    </ul>

    <input type="submit" value="Send selected items"/>
</form>

Finally a success view to show which items the user have selected:

@model List<SandboxMvcApplication.Models.Item>

@{
    ViewBag.Title = "Success";
}

<h2>Success: Selected items were</h2>

<ul>
@foreach (var item in Model)
{
    <li>@item.Id => @item.Title</li>            
}
</ul>
Community
  • 1
  • 1
seixasfelipe
  • 161
  • 1
  • 7
  • Hi Felipe thanks for the response. I need to obtain the ActionResult return so that I can pass it as an argument to a jquery function that will display the page returned in a dialog rather than change the current webpage. So previously I used: onclick ="'myjquery.showInDialog(Url.Action("CheckItem", new { isCorrect = true, id = 2}))'" This then showed the webpage in a popup. But now i have to submit a form at the same, so i was wondering how i would be able to do this and get the response back. – TBD Mar 09 '12 at 10:48