1

So, I have a view with a chosen search box, a button "Add" (btn-default) and a button "Edit" (breadcrumb) . When I click the Add button, the ajax sent me a table with the values (in this case, funcionaries) selected in the chosen text box.

I want that, when I click on the Edit button, send the chosen values (can be one, or hundreds of values) to another controller to return another view.

Don't want to use ajax because I want to use a new view on totally.

On the controller side, when I send the data with javascript, I always get null. Why?

View

<script>
$(document).ready(function () {
    $(".btn-default").on("click", function (event, params) {

        $.ajax({
            url: '@Url.Action("EditarPonderacoesEspecial", "Sorteios")',
            type: 'POST',
            dataType: 'html',
            cache: false,
            traditional: true,
            data: { bdoIds: $(".chosen-select").val() },
            success: function (responseText, textStatus, XMLHttpRequest) {
                $("#MyDiv").empty();
                $("#MyDiv").html(responseText);
            },
            error: function () { }
        })
    });

    $(".breadcrumb").on("click",function (event, params) {
            bdoIds = $(".chosen-select").val();
            $.post("/Sorteios/EditarPonderacoesEspecialSecond/", bdoIds);
    });
});

Controller

public ActionResult EditarPonderacoesEspecialSecond(string[] bdoIds)
{ 
    //do whatever I want with the bdoIds
    return View();
}

I had tried many different ways, but the controller always receive the parameter as null. What I am doing wrong? Thanks!

2
  • You could use something like JQuery to aggregate the selected values and then insert them into a hidden form and then submit the form. That way you are not using AJAX and the form can redirect the user to the other view you want to return. Commented Feb 27, 2015 at 12:21
  • @SimonH, sounds interesting! Do you have some tutorial for that? Commented Feb 27, 2015 at 12:23

2 Answers 2

1

Your controller action is expecting an array of strings.

Assuming .chosen-select is a select list as that part is missing from the question.

First read the selected values into an object as follows:

var selectedValues = [];
$(".chosen-select :selected").each(function() {
  selectedValues.push($(this).attr('value'));
});

Then send them as follows:

$(".breadcrumb").on("click",function (event, params) {
    var selectedValues = [];

    $(".chosen-select :selected").each(function() {
           selectedValues.push($(this).attr('value'));
    });

   $.post("/Sorteios/EditarPonderacoesEspecialSecond/", { bdoIds: selectedValues  });
});
Sign up to request clarification or add additional context in comments.

3 Comments

Hutchonoid, where I send the data? In ajax? the ajax is for the other button, not for what I want! Should I remove the ajax part from the post?
@danielpm See update, is .chosen-select a select list?
Perfect! Hutchonoid, you're the best! Thank you so much!
0
  1. Declare Global array like

    var SelectedArray = new Array();
    
  2. When you select multiple selectlist item each time push value in SelectedArray

    $('#ChosenId').chosen().change(function () {
    SelectedArray = $('#ChosenId').chosen().val();
    

    });

  3. Then your ajax data is like

    data: { bdoIds: SelectedArray },

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.