I am attempting to set up a PartialView that displays a list of objects, which contain a boolean property that the end user will check off. Upon submitting, the PartialView should perform an AJAX POST in order to pass that list of objects to another PartialViewResult and display another PartialView on the same page.
However, my controller is getting a null value for that list. How Do I fix this?
Simple Example:
View:
<div id="divNumOne">
<!-- Where the first PartialView is Displayed -->
</div>
<div id="divNumTwo">
<!-- Where the second PartialView should be Displayed -->
</div>
PartialViewOne:
@model MyApplicationName.Models.SearchList
<script src="~/Scripts/jquery-3.3.1.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
@{using (Ajax.BeginForm("PartialView2", "ControllerName", null, new AjaxOptions()
{
HttpMethod = "POST",
UpdateTargetId = "divNumTwo",
InsertionMode = InsertionMode.Replace
}, new
{
id = "partialViewOneSubmitForm"
}))
{
@Html.AntiForgeryToken()
<table>
<thead>
<tr>
<th>Select</th>
<th>ColumnOne</th>
<th>ColumnTwo</th>
<!-- etc. ... -->
</tr>
</thead>
<tbody>
@foreach (var item in Model.SearchResultList)
{
<tr>
<td>
@Html.CheckBoxFor(modelItem => item.Select)
</td>
<td>
@Html.DisplayFor(modelItem => item.ColumnOne)
@Html.HiddenFor(modelItem => item.ColumnOne)
</td>
<td>
@Html.DisplayFor(modelItem => item.ColumnTwo)
@Html.HiddenFor(modelItem => item.ColumnTwo)
</td>
<!-- etc. ... -->
</tr>
}
</tbody>
<!-- etc. ... -->
<button type="submit" class="btn btn-outline-primary" style="font-weight:500;">Lock in</button>
Model:
public class SearchList
{
public List<SearchResult> SearchResultList { get; set; }
}
public class SearchResult
{
public bool Select { get; set; }
public string ColumnOne { get; set; }
public string ColumnTwo { get; set; }
// etc. ...
}