I am using MVC to create a web page. I my View, I use JavaScript to generate a list of IDs and whether that are valid. For each pair, I end up with two JS varables such as:
var id = 123;
var isValid = true;
I am currently storing that data in a JS Map and just do:
var myJSMap = new Map();
myJSMap.set(id, isValid);
to insert the sets of data.
I then want to send this data to my C# Controller Action. My Controller Action method, however, is expecting a Dictionary Object:
public virtual ActionResult MyAction(Dictionary<int, bool> dictionary){
return View();
}
Since C# is a server-side and JS is a client-side language, I am having problems sending data from one to the other. I tried different options to somehow send my JS map data from my View to my Controller, but so far have not been able to get any of them to work. Are any of my options below doable, or is there another way to send a group of pairs of JS data to a C# Controller Action method?
Option 1:
Store my JS variables in a JS Map like I currently am doing. Then, is there a way to convert my JS Map to a C# Dictionary (either copy all the values or somehow do it while sending it to my Controller like with ajax)?
Option 2:
Create a Dictionary<int, bool> in my View right away. Then, instead of inserting my two JS variables into a JS Map, can I somehow insert my two JS variables into a C# Dictionary? I would basically want to do @myCSDictionary.Add(id, isValid); using Razor syntax, yet can't since id and isValid are JavaScript variables.
Option 3:
Instead of using a JS Map and a C# Dictionary, is there a different data type (that can store pairs of a data) I can use that matches up in both languages so that I would be able to just send the JS Object to my Controller using Ajax like you can do for int and string?
Aucun commentaire:
Enregistrer un commentaire