1

if I want to pass key/value pairs from javascript to C# ASP.NET code behind, should I use querystring parameters, hidden value or other methods? Values are not from the form, so jquery serialize or params() won't work..I guess I have to serialize it manually?? Values come from dropdownlist (name/selected option text) pairs

1
  • What kind of C# code (ASP.NET? MVC?) -- and where do the values come from? Commented Jun 29, 2011 at 19:47

5 Answers 5

2

That's what JSON is for. Most Javascript frameworks have JSON serializer built-in. Probably C# has a JSON module as well (but I don't know). You could also use XHR to send it.

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

Comments

1

The easiest, most universally compatible way to do this is by appending the key-value-collection to the querystring. It will be available in ASP.NET from HttpContext.Request.QueryString.

That being said, there are a variety of ways to accomplish your goal. You could send an $.ajax request using the serialized values. If you want to get the serialized values in jQuery by serializing the form, you can add elements to the form prior to serializing.

2 Comments

can you show me a quick example how to append querystring values in js?
@stewiegriffin, Check out jsuri. It is a fluent JavaScript library for parsing and manipulating Uri strings. It is pretty powerful, and the particular method to do what you want is addQueryParam(key, value[,index]). The source of the string manipulation can be found at code.google.com/p/jsuri/source/browse/jsuri.js#328 .
0

probably best to pass it as JSON and then use a JSON library (or WCF) to convert it

Comments

0

You can achieve this by having a hidden server tag and include an onclientclick attribute on your postback button(s).

<input id="hdnKV" runat="server" />
<asp:Button id="btnSubmit" runat="server" onclientclick="selectKeyValues();" onclick="btnSubmit_OnClick">Submit</asp:Button> 

This onclientclick javascript function would populate this hidden value from your key value pairs.

function selectKeyValues() {

    var kv= getKeyValues(),
        hiddenValue = document.getElementById('<%= hdnKV.ClientID =>');

    hiddenValue.Value = kv;

}

It's not stated what data you have but you can probably use the following format for keyvalue pairs.

key1=value1,key2=value2,key3=value3

1 Comment

this is easy way to send it over C# code, but ugly to deserialize it in the code behind
0

I had the same issue. I solve it by something like this :

I prepare my array inside of a foreach loop in JavaScript

var itemIdVersionCollection = [];
var itemName = row[1];
var version  = row[2];
var key = { Key: itemId , Value: version };
itemIdVersionCollection.push(key);

My Model was something like this :

public class IdVersion
{
    public int? Key { get; set; }
    public string Value { get; set; }
}

public class SomeModel 
{
   public List<IdVersion> ItemIdVersion { get; set; } 
}

The action :

Public ActionResult MethodName(SomeModel model)

Comments

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.