1

When I use JQuery to serialize a list of id's for my querystring it produces a url like:

mydomain/mypage?id=1&id=2&id=3&id=4

Here is what my form looks like

<div><input type="text" name="id" value="1" /></div>
<div><input type="text" name="id" value="2" /></div>
<div><input type="text" name="id" value="3" /></div>
<div><input type="text" name="id" value="4" /></div>
<div><input type="text" name="id" value="5" /></div>

<div><a href="#" id="myLink">CLICK ME!</a></div>
<script>
$('#myLink').click(function() {

//see: http://api.jquery.com/serialize/
myids = $('input[name=id]').serialize();

alert( myids );
return false;

});
</script>

-live demo - http://jsfiddle.net/yDpu6/2/


When trying to get the querstring values from my controller ActionResult I only get the first id (eg: 1) but when using Request.Quesrtring["id"] I get all the id's (eg:: 1,2,3,4).

Is there a way that I can pass all the serialized id's into my controller without using Request.Quesrtring["id"]?

4

1 回答 1

0

如果您假设下面的代码将给出所有值,那么您的假设是错误的

var value = ('.classname').val();

您需要在发布 ID 之前将值推送到数组。

查看代码

<input type="text" class="idList" value="1"/>
<input type="text" class="idList" value="2"/>
<input type="button" value="Post Values" onclick="javascript:postValues()" />
<script type="text/javascript">
function postValues() {
    var values = [];
    $('.idList').each(function () {
        values.push($(this).val());
    });

    postData = { id: values }

    $.ajax({
        type: "POST",
        url: "/Home/PostIds",
        data: postData,
        dataType: 'json',
        success: function (data) {
            alert($('.idList').val());
        },
        traditional: true
    });
}
</script>

动作方法代码

public ActionResult PostIds(List<string> id)
{
    return null;
}
于 2013-02-24T08:37:01.630 回答