How can I input items (selectedPublishersList, selectedTranslatorsList, selectedAuthorsList) in this action? I have tried to pass the data from the view to the action, but the received value is always null in the controller.
C#
[HttpPost]
public async Task<ActionResult> Create(CreateBookViewModel model)
{
var bookCategory = await _bookCategoryService.GetById(model.CategoryId);
var selectedAuthors = Request.Form["selectedAuthors"].ToString();
var selectedPublishers = Request.Form["SelectedPublishers"].ToString();
var selectedTranslators = Request.Form["SelectedTranslators"].ToString();
//model.CategoryId = CategoryId;
model.Category = bookCategory.Name;
model.Authors = selectedAuthors.Split(',').ToList();
model.Publishers = selectedPublishers.Split(',').ToList();
model.Translators = selectedTranslators.Split(',').ToList();
if (ModelState.IsValid)
{
var result = await _bookService.Create(model);
if (result.IsSucceeded)
{
return RedirectToAction("Index", result);
}
else
{
ModelState.AddModelError(string.Empty, "خطا در ایجاد کتاب.");
}
}
//return View(model);
return RedirectToAction("Create");
}
I attempted to use hidden inputs, but I was unable to retrieve the data in the action due to my limited knowledge of javascript.
HTML
<!-- HTML code goes here -->
In this particular code snippet, I made an effort to add values for each list. If duplicates are encountered, an error message will be displayed. Deleted items can also be added back, and the recorded data will be sent to the action in C#.
JavaScript
// JavaScript code goes here