This question already has an answer here:
- Post an HTML Table to ADO.NET DataTable 2 answers
I have a list that I would like to utilize checkboxes to select different elements. In my view, the information in passed in correctly, and I get the checkboxes, but when it is saved, the list is empty. I could use some help.
public struct SelectedCategories
{
public int categoryID;
public string PubNum;
public string category;
public bool selected;
}
public class CategoryViewModel
{
public List<SelectedCategories> current_categories_list { get; set; }
}
My controller
public class CategoryController : Controller
{
// GET: Category
public ActionResult Index()
{
List<SelectedCategories> CategoryList = new List<SelectedCategories>();
CategoryList.Add(new SelectedCategories { categoryID = 1, PubNum = "one", selected = false, category = "first" });
CategoryList.Add(new SelectedCategories { categoryID = 2, PubNum = "two", selected = false, category = "second" });
CategoryList.Add(new SelectedCategories { categoryID = 3, PubNum = "three", selected = false, category = "third" });
var viewModel = new CategoryViewModel()
{
current_categories_list = CategoryList
};
return View(viewModel);
}
[HttpPost]
public ActionResult SaveCategories(CategoryViewModel viewModel)
{
return Json(new { Result = "Save Failed " });
}
}
}
My view
@using (Ajax.BeginForm("SaveCategories", "Category", new AjaxOptions {HttpMethod = "POST", OnSuccess = "FormSubmitSuccess", OnFailure = "FormSubmitFailure" }))
{
<h3> Categories</h3>
<div id="table-scroll">
<table class="table-striped">
<tbody>
@foreach (var item in Model.current_categories_list)
{
<tr>
<td>@Html.DisplayFor(m => item.category)</td>
<td>@Html.CheckBoxFor(m => item.selected)</td>
</tr>
}
</tbody>
</table>
</div>
<input id="submitform" type="submit" value="Save Data" style="width: 100px" class="btn btn-success" />
}
In the "save Failed" section, it shows a null value for the list.
Aucun commentaire:
Enregistrer un commentaire