0

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]" />&nbsp;</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>

`

11
  • A little confused, if you are wanting to return a view instead of use the same page, why not make the original form post to that page with the same logic as your select list page? I do not see how ajax is needed for your current issue. Commented Mar 18, 2020 at 13:39
  • 1
    why are you trying to use ajax? you are going from one view to another. so just post to your controller. populate your viewbag or model and return the other view Commented Mar 18, 2020 at 13:41
  • 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. Commented Mar 18, 2020 at 13:45
  • @BryanDellinger Well, I after I select checkbox 1 and 2 and I get the record ID , I must return to my controller to concertanate the 5 fields of students 1 and student 2 based on the record ID, I then take those fields (lets use name as an example) and write name for student 1 and student 2 in a new variable and then display that in a next view. I'm using ajax to send the IDs in and then do my work on it. Commented Mar 18, 2020 at 16:24
  • @Danimal I'm actually unsure how to return that array to my controller without using ajax Commented Mar 18, 2020 at 16:25

1 Answer 1

1

If I am understanding this correctly you just need to post to the page where the logic ins in home/createmany?

You dont need ajax, just a form

<form action="/form/createMany" method=""Post" >
 <!-- you need your check boxes, keep them all the same name -->     
 <input type="checkbox" name="MyChecks" value="Bob" /> Bob<br/>
 <input type="checkbox" name="MyChecks" value="Karen" />Karen</br> 
  ... 
 <!-- then your submit -->
 <input type="submit" name="SUB" value="CREATE MANY" />
</form>

After you add whatever else you want in the form then you can go to the controller for createmany. You can use the submit button value to process only if data is being passed from this form

string fromForm = Request.Form["SUB"];
if(fromForm == "CREATE MANY"){
    string names = Request.Form["MyChecks"];  //should return selected as CSV
    string[] myArr = names.Split(',');
    // add your logic here from your ajax call   
}
Sign up to request clarification or add additional context in comments.

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.