0

I was looking around and trying to find a solution for my problem, but somehow I can not find the right answer.

I have an Index.cshtml view that looks like this:

@model IEnumerable<MyProject.Models.Conditions>
    
@{
    ViewBag.Title = "Index";
}
@using (Html.BeginForm())
{
  <style> ... </style>
  <div class="container1">
        <div class="box1">
         <div class="box-cell1 box1">
                <fieldset id="field1">
                    <legend>CustomerNumber</legend>
                    @Html.TextBox("s_custNum")
                    <input type="submit" value=">" style="height: 22px; width:  50px; padding:0px;" />
                </fieldset>
            </div>
        </div>
    </div>

<table class="tableMain" id="x">
        <tr class="trMain">
            <th class="thMain">
                @Html.DisplayNameFor(model => model.ID)
            </th>
            <th class="thMain">
                @Html.DisplayNameFor(model => model.NAME)
            </th>
            <th class="thMain">
                @Html.ActionLink("Edit Lines", "EditAll", "EditAll",  null)
            </th>
        </tr>

         @foreach (var item in Model)
         {
            <tr class="trMain">
                <td class="tdMain">
                    @Html.DisplayFor(modelItem => item.ID)
                </td>
                <td class="tdMain">
                    @Html.DisplayFor(modelItem => item.NAME)
                </td>
               <td class="tdMain">
                    <input type="checkbox" class="chkCheckBoxId"  name="custId" value="@item.ID" />
                </td>
            </tr>
        }
    </table>

I'm currently listing all records as table on my site and each record has a checkbox. The header has an action link for redirecting the user to a new editing site. At the top of the site is a search Textbox.

The method in my controller looks like this:

    [HttpPost]
    public ActionResult EditAll(FormCollection collection)
    {
        var test = Request.Form.GetValues("custId");
        string[] ids = collection["custId"].Split(new char[] { ',' });

        return View();
    }

Here I'm trying to get all the ids from the records that are checked. However, I get an error 404 that the page can not be found. When I remove the [HttpPost] attribute, the method gets called but returns no values in "test" or "ids".

I even tried to add values to my Html.BeginForm, but then the searchbar on the site does not work anymore.

Could someone help me retrieving the ids of the checked records? I'm new to ASP.NET MVC.

Many thanks!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Canox
  • 557
  • 1
  • 7
  • 20

1 Answers1

0

The action link you created creates an anchor tag (https://www.w3schools.com/tags/tag_a.asp)

@Html.ActionLink("Edit Lines", "EditAll", "EditAll", null)

Anchor tags are a GETs, not POSTs.

What you are looking for since you already have a BeginForm() and an input button -- which would cause your submit button to post back to the "Index" action -- is maybe doing this in Javascript or putting multiple forms on the page.

Forms cannot be nested inside of each other, but you can have multiple on a page.

Alternatively, just change it to an [HttpGet] if that's what you are trying to do.

Edit: Javascript example How to send data in jquery.post to mvc controller which use ViewModel as parameter?

var myData = {
          Parameter1: $("#someElementId").val(),
          Parameter2: $("#anotherElementId").val(),
          ListParameter: { /* Define IEnumerable collections as json array as well */}
          // more params here
         }  
$.ajax({
    url: 'someUrl',
    type: 'POST',
    dataType: "json",
    contentType: 'application/json',
    data: JSON.stringify(myData)
});  


[HttpPost]
public JsonResult Create(CustomViewModel vm)
{
    // You can access your ViewModel like a non-ajax call here.
    var passedValue = vm.Parameter1;
}
Kyle Crabtree
  • 198
  • 1
  • 10
  • Using a HttpGet does call the method but my formsCollection is still empty. How to do this over java script in this case? I would need to open a new page by clicking the link where my checked Ids are passed. Because the user then should edit a certain value and presses another button to update the datatabse. – Canox Nov 23 '22 at 12:34
  • Just added an example. You could pull each value (assuming you are using jQuery). – Kyle Crabtree Nov 23 '22 at 12:39
  • I tried the approach with two forms but somehow this does not work. I wrapped my table into
    and it still does not work :( What are the parameters in your example? Since every record has a checkbox I would need all records which are checked.
    – Canox Nov 25 '22 at 06:18