Goal
Currently, I have a program with the following goal. The program loads a table with X rows where each row has a checkbox and when I select then and click a button the value in the fields is written to one field in the resulting view.
Example if I have a name field with the names Bob, John and Karen, if I select checkbox 1 and 3, the names Bob and Karen is written to my new text field in the next view.
How far I have gotten
So far, I have my table loaded and when I select the checkbox the row ID is passed to a controller where I have a look which selects the name and writes it to a new variable on each iteration.
Problem
The problem I am facing is that, at the end of all this, When I attempt to open my new view, despite the method being called the new view doesnt load on the page and there is no error displayed. I suspect this is because of my ajax being used to call the controller and there is no sucess function but I am unsure how I can do what I want to ajax. See code.
Tableview.cshtml Explaination - Basically what happens here is, I get every checkbox that has been checked and I pass it to the selectMany function in Home controller. This works fine.
`
var SaveList = function () {
// alert("Here");
var arrItem = [];
var commaseperatedIds =
$("#projectsTable tr td input[type=checkbox]").each(function (index, val) {
debugger
var checkId = $(val).attr("Id");
var arr = checkId.split('_');
var currentCheckboxId = arr[1];
var Ischecked= $("#" + checkId).is(":checked", true);
if (Ischecked) {
arrItem.push(currentCheckboxId);
}
})
if (arrItem.length != 0) {
commaseperatedIds = arrItem.toString();
$.ajax({
url:"/Home/SelectList",
type: "POST",
data: { ItemList: commaseperatedIds },
sucess: function (response) {
//Code here
}
})
}
}
Home Controller- SelectList Explanation- Here, I get the Id's and for each ID I run a selection and take the name field. What I want to accomplish here is, for each checked checkbox, I take the name, and append it into one name and I load that name with both names on the resulting view but under no circumstances can i get the view to show.
[HttpPost]
// public JsonResult SelectList(string ItemList)
public ActionResult SelectList(string ItemList)
{
string[]arr = ItemList.Split(',');
string name;
foreach(var id in arr)
{
var currentId = id;
var Report = from t in db.classList
select new
{
t.name
};
name = Report.Select(e => e.name).FirstOrDefault();
}
// return RedirectToAction("CreateMany");
// return View("CreateMany");
return View("/Home/CreateMany");
// return Json("", JsonRequestBehavior.AllowGet);
}
SelectMany Explanation - and finally the last function which is just a return view.
public ActionResult CreateMany()
{
return View();
}
Grateful if I could be pointed in the right direction.
Regards
EDIT
`
@model IEnumerable<Generator.Models.ACT_activitiesDescr>
@{
ViewBag.Title = "Activities";
}
<head>
<link href="//cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css" rel="stylesheet" type="text/css" />
<script src="//cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
</head>
@*<link href="@Url.Content("~/Scripts/datatables/jquery.dataTables.min.css")" rel="stylesheet" type="text/css" />*@
@*<!-- Page JS Plugins -->
<script src="~/Scripts/datatables/jquery.dataTables.min.js"></script>
<!-- Page JS Code -->
<script src="~/Scripts/datatables/base_tables_datatables.js"></script>*@
<h3><u> </u></h3>
@*<p style="font-size:25px">
@Html.ActionLink("Add New Task", "Create")
</p>*@
<button type="button" class="button" id="button">Click Me!</button>
<a class="btn btn-success btn-block" onclick="SaveList()"> Open</a>
<table id="projectsTable" class="display table table-bordered table-primary ">
@*<table id="projectsTable" class="display" style="width:100%">*@
<thead>
<tr>
<th>
Select
</th>
<th>
@*@Html.DisplayNameFor(model => model.REF_Projects.Project)*@
Task
</th>
<th>
Type
</th>
<th>
Depart
</th>
<th>
Report Period
</th>
<th>
Submission Date
</th>
<th>Submitted By</th>
<th>Details</th>
</tr>
</thead>
@foreach (var item in Model)
{
<tr style="color:black;">
<td><input type="checkbox" id="[email protected]" name="[email protected]" /> </td>
<td>
@*@Html.DisplayFor(modelItem => item.REF_Projects.Project)*@
@if (item.proAct == "Project")
{
@Html.DisplayFor(modelItem => item.REF_Projects.Project)
}
else
{
@Html.DisplayFor(modelItem => item.REF_Activities.Activity)
}
</td>
<td>
@Html.DisplayFor(modelItem => item.proAct)
</td>
<td>
@Html.DisplayFor(modelItem => item.REF_Units.location)
</td>
<td>
@Html.DisplayFor(modelItem => item.reportPs) - @Html.DisplayFor(modelItem => item.reportPe)
</td>
<td>
@Html.DisplayFor(modelItem => item.enterDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.USER_users.fname) @Html.DisplayFor(modelItem => item.USER_users.lname)
</td>
<td>
<button class="btn btn-sm btn-default" type="button" onclick="location.href='@Url.Action("Edit", "Act", new { id = item.actID })'" data-toggle="tooltip" title="Details">@*<i class="fa fa-list-ol"></i>*@View Details</button>
@*@Html.ActionLink("Edit", "Edit", new { id = item.actID }) |
@Html.ActionLink("Details", "Details", new { id = item.actID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.actID })*@
</td>
</tr>
}
</table>
<script>
var SaveList = function () {
// alert("Here");
var arrItem = [];
var commaseperatedIds =
$("#projectsTable tr td input[type=checkbox]").each(function (index, val) {
debugger
var checkId = $(val).attr("Id");
var arr = checkId.split('_');
var currentCheckboxId = arr[1];
var Ischecked= $("#" + checkId).is(":checked", true);
if (Ischecked) {
arrItem.push(currentCheckboxId);
}
})
if (arrItem.length != 0) {
commaseperatedIds = arrItem.toString();
$.ajax({
url:"/Act/SelectList",
type: "POST",
data: { ItemList: commaseperatedIds },
sucess: function (response) {
//Code here
}
})
}
}
</script>
`
if I select checkbox 1 and 3, the names Bob and Karen is written to my new text field in the next view.just use form post. The purpose of ajax is not to refresh the page. It seems that your goal is you want to redirect them.