I am having some trouble with my code. Okay so I have an ASPx webpage. Right now i have drop down box that gets filled by a sql server from the model (via the controller). I have added a button and dropdownlist like below.
I am unable to return the selected value from the dropdownlist to the controller. Any help would be great
View:
<% = Html.DropDownList("Books") %>
<input type = "submit" value = "Update"/>
<h1> <%=TempData["BookName"]%> </h1>
Controller:
public ActionResult Index(string bookName)
{
ViewData["BookName"] = new SelectList(_context.BookName.Select(a =>a.Book_Name).Distinct());
if (!string.IsNullOrWhiteSpace(bookName))
{
ViewData["Books"] = _context.BookName.Where(b => b.Book_Name == bookName).ToList();
UpdateBookNameTitle(bookName);
}
return View();
}
public ActionResult UpdateBookNameTitle(string bookName)
{
TempData["BookName"] = bookName;
return View("Home2");
}
You need to wrap you drop-down list and button into form (in view). Also change the name of drop-down to “bookName”, so MVC can map selected value to
bookNameaction parameter:Without HTML form your submit button will not do POST requests to the server.