1

Being a newbie in ASP.NET MVC framework, I am stack in sending a list of my model back to the controller via POST method. Below are samples of my source code and their explanation.

Model Class:

public class SiteIdentifier
    {
        [Display(Name = "Configuration Id")]
        public string ConfigurationId { get; set; }

        [Display(Name = "County")]
        public string County { get; set; }
    }

Controller (Get Method):

public ViewResult ViewNewSites()
{
    List<SiteIdentifier> sites = new List<SiteIdentifier>()
    {
        new SiteIdentifier{ConfigurationId = 1, County = "County1"},
        new SiteIdentifier{ConfigurationId = 2, County = "County2"},
        new SiteIdentifier{ConfigurationId = 3, County = "County3"}

    };

    return View(sites);
}

View:

In this view, the user has to select some rows using checkbox and click on the submit button and the submit button has to pass those checked rows to controller.

@using HIE_Management_Studio.Common
@model IEnumerable<SiteIdentifier>

<!--If there is nothing to show, do not render this place.-->
@if (Model != null)
{
    using (Html.BeginForm("CopyToProductionPOST", "CopyBySite",
            new AjaxOptions
            {
                InsertionMode = InsertionMode.Replace,
                HttpMethod = "POST",
            }))
    {
        <div class="row">
            <div class="col-md-12">
                <div class="alert alert-info">
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th>
                                <input type="checkbox" id="chkSelectAll" />
                            </th>
                            <th>
                                @Html.DisplayNameFor(model => model.ConfigurationId)
                            </th>
                            <th>
                                @Html.DisplayNameFor(model => model.County)
                            </th>
                        </tr>

                    </thead>
                    <tbody>
                        @foreach (var item in Model)
                        {
                            <tr>
                                <td class="text-nowrap">
                                    @Html.CheckBoxFor(modelItem => item.Selected, new { @class = "chkSelect" })
                                </td>
                                <td>
                                    @Html.EditorFor(modelItem => item.ConfigurationId)
                                    @Html.HiddenFor(modelItem => item.ConfigurationId)
                                </td>
                                <td>
                                    @Html.EditorFor(modelItem => item.County)
                                    @Html.HiddenFor(modelItem => item.County)
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        </div>

        <div class="row">
            <div class="col-md-12">
                <input type="submit" value="Submit" class="btn btn-info" />
            </div>
        </div>

        <script>
            $("#chkSelectAll").bind("change", function () {
                $(".chkSelect").prop("checked", $(this).prop("checked"));
            });
            $(".chkSelect").bind("change", function () {
                if (!$(this).prop("checked"))
                    $("#chkSelectAll").prop("checked", false);
            });
            $(".alert").hide().fadeIn("slow");
        </script>
    }
}

Controller (Post Method)

Now here is the post method in my controller that has to get all the selected rows from the view.

[HttpPost]
public ViewResult CopyToProductionPOST(List<SiteIdentifier> formCollection)
{
     SiteIdentifier siteIdentifier = new SiteIdentifier();
     // siteIdentifier.ConfigurationId = formCollection[0]["ConfigurationId"];

     return View("CopyBySite");
}
M. Fawad Surosh
  • 450
  • 7
  • 20

1 Answers1

1

I tried below link and it worked :)

https://www.c-sharpcorner.com/article/pass-dynamically-added-html-table-records-list-to-controller/

M. Fawad Surosh
  • 450
  • 7
  • 20
  • 1
    This link was very much helpful but here it is done via EditorFor but if I am using DisplayFor it is not passing the data. Do I need to send it as Hidden Data with EditorFor? As my usecase doesn't require the admins to edit the data. – Lucifer Sep 21 '22 at 19:30