How to pass array using asp-route? - c#

Form returns url format: localhost2343/index?Colors=red&Colors=blue&Colors=pink
asp-route return url format: localhost2343/index?Colors=red,blue,pink
If I use form submit button than everything seems good. But If i click on sort hyperlink than it will pass URL Colors=System.String%5B%5D
How can I pass value of Colors inside asp-route?
<form asp-page="./index" method="get">
<select asp-for="Colors" asp-items="#Model.Colors_SELECT" class="MultiSelect" multiple>
<option value="">All</option>
</select>
...
</form>
<Table>
...
<a asp-page="./Index" method="get"
asp-route-SortOrder="#Model.Colors_Sort"
asp-route-SearchString="#Model.SearchString"
asp-route-Colors="#Model.Colors">
#Html.DisplayNameFor(model =>
model.MyList[0].Colors)
</a>
...
</table>
[BindProperty(SupportsGet = true)]
public string[]? Colors { get; set; }
public SelectList? Colors_SELECT { get; set; }
public async Task OnGetAsync()
{
// Form - URL Format
// get values from URL & set inside selectlist
var result = Request.Query["Colors"];
var i = 0;
foreach (string? item in result) {
Colors[i] = item;
i++;
}
}
Update - I tried this but on sort link, it removes Sort variable & it picks only 1 Colors (not multi)
<a asp-page="./Index" method="get"
asp-route-SortOrder="#Model.Colors_Sort"
asp-all-route-data="#Model.routeData">
[BindProperty(SupportsGet = true)]
public Dictionary<string, string> routeData { get; set; }
....
var routeData = new Dictionary<string, string>();
routeData.Add("SortOrder", CurrentSort);
routeData.Add("SearchString", SearchString);
for (int i = 0; i < result.Count; i++)
{
var myParam = result[i];
routeData.Add($"Colors{i}", myParam.ToString());
}

This may help:
To get the selected values from a select element in a Razor page, you can use the Request.Form["selectName"] collection.
For example, consider the following select element:
#page "{Colors}"
<form method="post">
<select multiple name="colors">
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="pink">Pink</option>
</select>
<button type="submit" asp-page-handler="GetColors">Submit</button>
</form>
To get the selected values in the form submission handler and modify the query string in the URL and include the selected values (red, blue, pink), you can try the following code:
public IActionResult OnGet(string? Colors)
{
if (string.IsNullOrWhiteSpace(Colors))
{
// do something
return Page();
}
if (!string.IsNullOrWhiteSpace(Colors))
{
// Do something
return Page();
}
return Page();
}
public IActionResult OnPostGetColors()
{
IDictionary<string, string> params = new Dictionary<string, string>();
var selectedColors = string.Join(",", Request.Form["colors"]);
params.Add("Colors", selectedColors);
string query = "";
foreach (var p in params)
query += $"{p.Key}={p.Value}";
var url = $"{HttpContext.Request.Path}?{query}";
return Redirect(url); // url : https://localhost:7272/index?Colors=red,blue,pink
}
This code will help you submit the selected values through the URL as a query string, and then will redirect the user to a new URL with the selected values added to the query string as multiple values for the colors parameter.

Related

Passing jQuery dictionary object to an input field

var dictionary = [];
dictionary.push({
key:"Res01" ,
value: "Loss of internet connection at location"
});
when adding this dictionary object to an input field
$('#hdnNotesDict').val('');
$('#hdnNotesDict').val(dictionary);
i am not getting the dictionary value in that input field.
getting result as: [object,object]
Thanks in advance and any suggestion will be appreciated
Let's say you have this form in your view:
<form method="post">
<input id="kvps" name="kvps" />
<input id="submit" type="submit" value="Submit" />
</form>
and you put some values like that:
(function () {
$('#submit').mousedown(function () {
let input = $('#kvps');
let dict = [];
for (var i = 0; i < 10; i++) {
dict.push({ key: `Res${i}`, val: `Val${i}` });
}
input.val(JSON.stringify(dict));
});
})();
in this case you convert the array in a string and you should take it as string into your controller
BUT - you cannot convert it to dictionary immediately, first you should map it to array of model with the same property names then you can call ToDictionary over it.
Array example:
[HttpPost]
public IActionResult JInput(string kvps)
{
var arr = JsonSerializer.Deserialize<object[]>(kvps);
return this.View();
}
If you need it as Dictionary<string, string> you schould use in your jquery object instead:
(function () {
$('#submit').mousedown(function () {
let input = $('#kvps');
let dict = {};
for (var i = 0; i < 10; i++) {
// watch out for the keys here to not overwrite the values
dict[`Res${i}`] = `Val${i}`;
}
input.val(JSON.stringify(dict));
});
})();
And in your controller like that:
[HttpPost]
public IActionResult JInput(string kvps)
{
var arr = JsonSerializer.Deserialize<Dictionary<string, string>>(kvps);
return this.View();
}

how to pass html option id or name or value from one view to another view's controller in MVC c#

I'm displaying dropdown list using html. then when a user select a item from the list that item's id or name or something should pass to my next view's controller.
this is my code...
***** this is my first controller *****
public ActionResult searchView()
{
XElement cruProductRoot = XElement.Load(Server.MapPath("~/XmlFiles/Cruisedata/cruiseprodutstwo.xml"));
var rootElement = cruProductRoot.Elements("CruiseProduct");//this is the root element
//for the location field
var getLocations = rootElement
.Select(l => l.Element("Location").Value)
.Distinct();
var getType = rootElement
.Select(t => t.Element("Types").Element("Type").Value)
.Distinct();
// Test productsTestone = new Test();
List<Test> productsLocation = new List<Test>();
List<Test> productsType = new List<Test>();
foreach (var iteml in getLocations)
{
productsLocation.Add(new Test
{
cruiseLocation = iteml.ToString()
});
};
foreach(var itemt in getType)
{
productsType.Add(new Test
{
cruiseType = itemt.ToString(),
});
}
ViewBag.Lc = productsLocation;
ViewBag.Tp = productsType;
return View();
}
*** and this is the view for the controller ****
#using (#Html.BeginForm("Test", "searchView", FormMethod.Post))
{
<div class="form-group" style="background-color:#808080; padding:30px;">
<div class="col-md-6" style="margin:10px;">
<label for="location">Destination </label>
<select id="location">
<option>Any</option>
#foreach (Test item in #ViewBag.Lc)
{
<option value=#item.cruiseLocation>#item.cruiseLocation</option>
}
</select>
</div>
</div>
******** this is my second controller ******
public ActionResult resultView(string value)
{
XElement sCruise = XElement.Load(Server.MapPath("~/XmlFiles/Cruisedata/cruiseprodutstwo.xml"));
var rootEle = sCruise.Elements("CruiseProduct")
.Where(s => s.Element("Location").Value == value);
foreach(var it in rootEle)
{
}
return View();
}
**** when user select an item from the list, and click submit then the selected item should send to the second view's controller. how can I do that.
if we use only links like this.
#Html.ActionLink(#itme.cruiseLocation,"resultView", new {name =#item.cruiseLocatin})
how to do for option also.I tried <option>#Html.ActionLink</option> also. help me with this
In first controller set
TempData["optionid"]=selectedid
And in second controller you can access that id from var a=TempData["optionid"]

Get the value of standard DDL list in mvc

I am using a method that generate my questions and answers i mean every questions appear in view and the answers of this question are put to the DDL as you can see here :
public string GenerateHtmlCode()
{
string result = string.Empty;
List<ExecutiveOfficer> executiveOfficers = executiveOfficerRepository.GetAll().ToList();
List<Indicator> indicators = indicatorRepository.GetAll().ToList();
foreach (ExecutiveOfficer executiveOfficer in executiveOfficers)
{
result += "<div style='width:100%;float:right;'><span>" + executiveOfficer.ExecutiveOfficerText +
"</span><span style='float:left'>" +
GenerateAnswer(indicatorRepository.FindBy(i => i.ExecutiveOfficerId == executiveOfficer.Id
).ToList(), executiveOfficer.Id) + "</span></div>";
}
return result;
}
In my create method in controller i pass the string to viewbag as you can see here :
[HttpGet]
public ActionResult Create(int preliminaryId)
{
ViewBag.preliminaryId = preliminaryId;
ViewBag.pageGenarator = objScoreRepository.GenerateHtmlCode();
// List<ExecutiveOfficer> executiveOfficer = objScoreRepository.GetAll().ToList();
//ViewBag.executiveOfficerName = new SelectList(executiveOfficer, "Id", "ExecutiveOfficerText");
return View("Create");
}
My html code :
</span><span style='float:left'><select id='1' name ='1'><option value='value1'>displaytext</option><option value='value2'>displaytext2</option></select></span>
As you can see here i have 1 DDL that has 2 values i need to get the value that is selected by the user i use the form collection to get the name of DDl as you can see here :
[HttpPost]
//[Authorize(Roles = "Expert")]
public ActionResult Create(FormCollection formCollection)
{
//objScoreRepository.Add(Score);
//objScoreRepository.Save();
foreach (var VARIABLE in formCollection.AllKeys)
{
}
return RedirectToAction("Create", "Score");
}
The formcollection just returns one record and the record is 1 the name of DDL .But i need to get the value of DDL.How can i do that?
My view:
#{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_LayoutIdeaOtherPage.cshtml";
}
<h2>Create</h2>
#using (Html.BeginForm())
{
#Html.ValidationSummary(true)
#Html.Raw(ViewBag.pageGenarator)
<div class="buttonPossion">
<input type="submit" Class="buttonSave" />
</div>
}
best regards .
You can use
foreach(string item in form.AllKeys)
{
var value = Request[item];
}

How can i create a SelectListItem() with Int Value

I have the folloiwng code inside my asp.net mvc action method:-
var CustomerData = customerlist.Select(m => new SelectListItem()
{
Text = m.SDOrganization.NAME,
Value = m.SDOrganization.ORG_ID.ToString(),
});
currently if i remove the ToString() from the ORG_ID , i will get an error that "can not explicitly convert long to string". so it seems that i have to define both the value and the text for the SelectListItem as strings. but since the SelectListItem should hold long , so is there a way to pass the values of the SelectListItem as long instead of strings?
...so is there a way to pass the values of the SelectListItem as long instead of strings?
No. And it doesn't make any sense to do so as when it is rendered, it's just HTML which has no concept of long.
If we have the action
public ActionResult Test()
{
var dictionary = new Dictionary<int, string>
{
{ 1, "One" },
{ 2, "Two" },
{ 3, "Three" }
};
ViewBag.SelectList = new SelectList(dictionary, "Key", "Value");
return this.View();
}
and the following view "Test.cshtml":
#using (Html.BeginForm())
{
#Html.DropDownList("id", ((SelectList)ViewBag.SelectList), "All")
<input type="submit" value="Go" />
}
The generated HTML is
<form action="/home/test" method="post">
<select id="id" name="id">
<option value="">All</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<input type="submit" value="Go">
</form>
and when we post to this action, text of your number is effectively parsed back into your desired type by the Model Binder
[HttpPost]
public ActionResult Test(int? id)
{
var selectedValue = id.HasValue ? id.ToString() : "All";
return Content(String.Format("You selected '{0}'", selectedValue));
}
And the above works as you might expect.

formcollection how to access html form associative array

i try to access a associative array in c#. The Array is send per post to my c# mvc web application.
e. g. html form
<Input Name="myArray[hashKey1]" value="123">
<Input Name="myArray[hashKey2]" value="456">
and in c# i need the keys and values - perhaps with data dictionary?!
[HttpPost]
public ActionResult Index(FormCollection collection)
{
Dictionary<string, string> = KEY, VALUE
...
}
i hope you can follow me :-/
Yes you can; but you need to specify the method of POST.
This doesn't work:
<form id="frmThing" action="#Url.Action("Gah", "Home")">
<input id="input_a" name="myArray[hashKey1]" value="123" />
<input id="input_b" name="myArray[hashKey2]" value="456" />
<input type="submit" value="Submit!"/>
</form>
This does:
<form id="frmThing" action="#Url.Action("Gah", "Home")" method="POST">
<input id="input_a" name="myArray[hashKey1]" value="123" />
<input id="input_b" name="myArray[hashKey2]" value="456" />
<input type="submit" value="Submit!"/>
</form>
Edit: To actually access the details in C#, in your example you'd do one of the following:
String first = collection[0];
String secnd = collection[1];
or
String first = collection["myArray[hashKey1]"];
String secnd = collection["myArray[hashKey2]"];
or even:
foreach (var item in collection) {
string test = (string)item;
}
Edit two:
Here's a trick you can use to get the behavior you want to see.
Firstly, define an extension method:
public static class ExtensionMethods
{
public static IEnumerable<KeyValuePair<string, string>> Each(this FormCollection collection)
{
foreach (string key in collection.AllKeys)
{
yield return new KeyValuePair<string, string>(key, collection[key]);
}
}
}
Then in your action result you can do this:
public ActionResult Gah(FormCollection vals)
{
foreach (var pair in vals.Each())
{
string key = pair.Key;
string val = pair.Value;
}
return View("Index");
}

Categories