2

post methot:

function PostChartValues(meter_id, range_type_id, start_date, end_date) {
var values = $("#type_selection").val();
//values = expected values as string array
$.ajax({
    url: '/Widget/GetMeterReadingsTimeSeries',
    type: 'POST',
    data: { MeterType: meter_id, DateRangeType: range_type_id, StartDate: start_date, EndDate: end_date, Parameters: values },
    beforeSend: function () {
        $("#chart_loading_div").show();
    },
    complete: function () {
        $("#chart_loading_div").fadeOut();
        $(".grids").jqGrid('GridUnload');
        grid(meter_id, range_type_id, $("#start_date").val(), $("#end_date").val());
    },
    success: function (result) {
        $("#chart").html(result);
    },
    error: function (result) {
        alert("Seçilen kritere uygun veri bulunamadı!");
    }
});   //end ajax
} //end PostChartValues

action method:

public ActionResult GetMeterReadingsTimeSeries(int MeterType, int DateRangeType, DateTime? StartDate, DateTime? EndDate,string[] Parameters)
{
    // ...
    return PartialView("_TimeSeries", chart);
}

I debugged it. only Parameters array is null. Is there an other way to post array with jquery post?

Thanks.

1
  • Have a look at this so-question Commented Nov 29, 2012 at 7:40

2 Answers 2

3

You can post selected values as string then you parse it back to array. I have created basic sample below,

Markup

<script src="~/Scripts/jquery-1.7.1.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('#someButton').click(function () {
            var selectedValues = [];
            $('#MyDiv input:checked').each(function () {
                selectedValues.push($(this).val());
            });
            console.log(selectedValues);
            $.ajax({
                url: 'someurl',
                type: 'POST',
                data: { values: selectedValues.join(",") }
            });
        });
    });
</script>
<button id="someButton">Do Ajax</button>
<div id="MyDiv">
    <input type="checkbox" value="test1" />
    <input type="checkbox" value="test2" />
    <input type="checkbox" value="test3" />
</div>

Controller

public class HomeController : Controller
    {       
       public void Test(string values)
        {
            string[] selectedValues = values.Split(',');
        }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

+1. "join" and "split" are good idea. Thanks. But I think, there may be a better way.
Yes it is, you can create form and model then send it to server via ajax, $("#someForm").serialize() gives you post data and you can handle it from controller action.. For sample pastebin.com/DVfvR0K7
Yes. I m allready using your suggestion. But I dont want to break my available scruct. Thanks again.
0

An alternate way to get the selected values if you're using Underscore:

var selectedValues = _.map($("#myDiv:checked"), function (item) { return $(item).val(); });

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.