I have a dropdownlist that is being populated by a sql server, I am using Visual Studio 2010, cshtml, with razor as well as using the MVC pattern to create this project. What I am trying to do is when someone selects a value from the dropdown list on change it will update the page with information about that book.
I need help with the three things below:
- user selects a book from the dropdownlist how to get the Book Name back to the controller
- The server (retrieve the information from the server about the book) and
- Back to view to be displayed.
I started with getting the dropdown poplulated.
My View looks like this
BookName: @Html.DropDownList("BookName", ViewData["BookName"] as IEnumerable<SelectListItem>, new { id = "UserSelectedValue" })
My Controller:
public ActionResult Index()
{
ViewData["BookName"] = new SelectList(_context.BookName.Select(a => a.Book_Name).Distinct());
return View();
}
A dropdown list can’t cause the page to post back to your controller on its own. You need to do one of two things:
Either way, you will need to wrap the dropdown/submit button in a form.
Option 1
Option 2
Your controller code would then become something like: