0

i have this list of hidden fields

   <input type="hidden" value="1" id="serviceslist" name="serviceslist" />
   <input type="hidden" value="2" id="serviceslist" name="serviceslist" />
   <input type="hidden" value="3" id="serviceslist" name="serviceslist" />

i have add more button which when clicked want to sent the serviceslist to mvc controller. basically i think that i need to send the list as array. how can i do it?

  $('#addmoreservices').click(function () {          
     var serviceslist= $("#serviceslist").val()
        $.ajax({
            url: url,
            data: { serviceslist: serviceslist },
            cache: false,
            dataType: "json",
            traditional: true,
            type: "POST",
            success: function (data) {

                alert("dddd");
            },
            error: function (reponse) {
                alert("error : " + reponse);
            }
        });
    });

this is my mvc controller

 [HttpPost]
    public ActionResult AddmoreServices(List<int> serviceslist)
    {

        return View();
    }
1
  • 1
    id should be unique Commented Feb 26, 2014 at 11:17

3 Answers 3

3

You have some errors in your html and js.

First of all, id must be unique:

<input type="hidden" value="1" id="val1" />
<input type="hidden" value="2" id="val2" />
<input type="hidden" value="3" id="val3" />

The second one, jquery val() function gets the current value of the first element in the set of matched elements, not array.

And the third one, is about posting your data on server. By default jquery.ajax post's your data in url contentType='application/x-www-form-urlencoded', it should be changed to application/json.

serviceslist = [$("#val1").val(), $("#val2").val() ,$("#val3").val()];
$.ajax({
   url: url,
   data: serviceslist,
   contentType: 'application/json',
   ....
Sign up to request clarification or add additional context in comments.

Comments

1

Alternatively you can do by making the button as submit like,

@using(Html.BeginForm("AddMoreServices", "Your controller name", FormMethod.Post, null)) {

<input type="hidden" value="1" id="val1" name="serviceslist" />
<input type="hidden" value="2" id="val2" name="serviceslist" />
<input type="hidden" value="3" id="val3" name="serviceslist" />

<button type="submit" value="Add More Services"/>

}

and your controller action method can be

[HttpPost]
public ActionResult AddMoreServices(FormCollection collection)
{
    try
    {
        string[] test = collection.GetValues("serviceslist");

        // here test[0] will give you the value of `val1`
        // test[1] will give `val2` and test[2] will give `val3`'s values
    }
    catch (Exception ex)
    {
        return null;
    }
}

Comments

0

Try something like this,

HTML :

<input type="hidden" value="1" class="serviceslist" name="serviceslist" />
<input type="hidden" value="2" class="serviceslist" name="serviceslist" />
<input type="hidden" value="3" class="serviceslist" name="serviceslist" />

JQUERY :

$('#addmoreservices').click(function () {          
    var serviceslist= $(".serviceslist").serialize();
    $.ajax({
        url: url,
        data: serviceslist,
        cache: false,
        dataType: "json",
        traditional: true,
        type: "POST",
        success: function (data) {

            alert("dddd");
        },
        error: function (reponse) {
            alert("error : " + reponse);
        }
    });
});

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.