Reputation: 3
I have done it with using Ajax post method .But my problem is that with Ajax we can not download a file so i needed a different idea.Following is my code
Asp.net MVC Ajax Post:
<script type="text/javascript">
$(document).ready(function () {
$('#btn').click(function () {
var vals = [];
$('input:checkbox[name=Blanks]:checked').each(function ()
{
vals.push($(this).val());
});
$.ajax({
// Check the value.
type: 'POST',
enctype: 'multipart/form-data',
url: '/Home/Extract',
// data: { 'name': checkboxData },
data: { 'name': JSON.stringify(vals) },
// contentType: 'application/json; charset=utf-8', // No need to define
contentType success: function (result) {
},
error: function (err, result) {
alert("Error in delete" + err.responseText);
}
});
});
return false;
});
</script>
@foreach (var m in (List<string>)ViewData["list"])
{
<ul>
<li>
<input type="checkbox" class="myCheckbox" name="Blanks" value="@m"/>
<img src="@m" alt=""/>
</li>
}
ASp.net MVC controller Section:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Extract(string[] name)
{
return view();
}
I neede without Ajax post method .Is it possible?
Upvotes: 0
Views: 9672
Reputation: 2343
You have a bunch of checkboxes with the same name and different values. You could post them to a method that takes a FormCollection, ie.
public ActionResult Test(FormCollection collection)
{
string results = collection["Blanks"];
}
This would give you a comma-delimited list of values (or null, where no checkboxes are ticked).
Alternatively, if you have the possible values as an array on the server then you could give the checkboxes names according to their array values, which would mean you could do something like this:
@using (Html.BeginForm("Test","Home"))
{
@Html.CheckBox("Blanks[0]", false);
@Html.CheckBox("Blanks[1]", false);
@Html.CheckBox("Blanks[2]", false);
<input type="submit" value="Submit" />
}
giving you an array of booleans in your Test method:
public ActionResult Test(bool[] Blanks)
{ }
Upvotes: 1