Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
can anyone help me how to pass list of integer values from view to mvc controller using url.action ??
Posted
Updated 4-Apr-20 7:27am

C#
public ActionResult Index(string[] data)
{
    return View();
}

XML
@{
    var qs = HttpUtility.ParseQueryString("");
    new List<string>{"one", "two", "three"}.ForEach(s => qs.Add("data", s));
}
<a href="@Url.Action("Index", "Home")?@qs">click</a>


Update:

XML
@{
    ViewBag.name = "xyz";
    ViewBag.data = new List<string>{"one", "two", "three"};

    var q = HttpUtility.ParseQueryString("");
    q.Add("name", ViewBag.name);

    foreach (var item in ViewBag.data)
    {
        q.Add("data", item.ToString());
    }

}
<a href="@Url.Action("Index", "Home")?@q">click</a>
 
Share this answer
 
v2
Comments
Member 11970999 28-Oct-15 7:17am    
Hi F-ES Sitecore,
@{
var a = new List<int>();

foreach (var item in ViewBag.data)
{
a.Add(item);
}

}

@Html.Raw(@Url.Action("index", "home", new { Ids= a, name= ViewBag.name}))

iam passing list like this to mvc controller thanks for solution but it doesnt work for me.
F-ES Sitecore 28-Oct-15 7:43am    
That's not going to work, I've updated my answer with an alternative solution.
Member 11970999 29-Oct-15 0:23am    
its not working
F-ES Sitecore 29-Oct-15 5:05am    
It works for me, so if you've changed or amended the code I posted in any way the issue is with how you have amended it.
Member 11970999 29-Oct-15 5:15am    
iam doing the same as in your solution i didnt change anything in that. in controller iam getting string[] data null
 
Share this answer
 
Comments
Member 11970999 28-Oct-15 7:13am    
i already tried it. its working for if the list have one value not for morethan one
gunjalgajjar 28-Oct-15 7:35am    
@{ var categories = new List<int>() { 6, 7 };

var parameters = new RouteValueDictionary();

for (int i = 0; i < categories.Count; ++i)
{
parameters.Add("category[" + i + "]", categories[i]);
}
}
< a href="@Url.Action(" test","home",="" parameters)"=""> click

Pass category as list of string

public ActionResult Test(List<string> category)
{
return View();
}
You can use

@Url.Action("Index", "home", new { data1 = 0, data2 = 0 })


SQL
public ActionResult Index(int data1, int data2)
{
    return View();
}


or

@Url.Action("Index", "home", new { data = yourintlist })


SQL
public ActionResult Index(int[] data)
{
    return View();
}
 
Share this answer
 
Comments
Member 11970999 28-Oct-15 7:23am    
hi,
present iam sending like this @Url.Action("Index", "home", new { data = yourintlist })
only but list was not pass to controller
Wessel Beulink 28-Oct-15 7:39am    
Normally you put this lists in a model and pass this to you view and pass the model to the controller again for changes. There is no way to create the list inside your controller?

You are using viebag.data to store the data. You can better use a ViewModel with a list inside.

Something like: public List<> a { get; set; }
You can call this model and fill it client sided with: @Model.a.add();


You need to use & for each item of the list.
Something like,
ids=1&ids=2&ids=3
C#
string param = string.empty;
foreach(var item in list)
{
    param += "ids=" + item + "&";
}
param = param.Remove(param.Length - 1); // remove the last appended &
@Url.Action("Action", "Controller", new { data = param });


-KR
 
Share this answer
 
v2
Instead Type Casting to “object” or “object[]” or using RouteValueDictionary. A simple way to achieve the same is using “Newtonsoft.Json”

If using .Net Core 3.0 or later;
Default to using the built in System.Text.Json parser implementation.
JavaScript
@using System.Text.Json;

…….

@Url.Action("ActionName", "ControllerName", new {object = JsonConvert.SerializeObject(‘@ModalObject’)  }))

If stuck using .Net Core 2.2 or earlier;
Default to using Newtonsoft JSON.Net as your first choice JSON Parser.
JavaScript
@using Newtonsoft.Json;

…..

@Url.Action("ActionName", "ControllerName", new {object = JsonConvert.SerializeObject(‘@ModalObject’)  }))

you may need to install the package first.
PM> Install-Package Newtonsoft.Json

Then,
C#
public ActionResult ActionName(string modalObjectJSON)
{
	Modal modalObj = new Modal();

	modalObj = JsonConvert.DeserializeObject<Modal>(modalObjectJSON);
	
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900