'how can I get value of dropdownlist From MVC View to Controller for creating an entry in table of database?
I am not sure how this works. code for View is :
@model ReservationSys.Models.Confirmed
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Confirmed</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.RoomNumber, "RoomNumber", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("RoomNumber", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.RoomNumber, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ReservationId, "ReservationId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("ReservationId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ReservationId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Reservation.Customer.CustomerName, "CustomerName", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("CustomerName", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Reservation.Customer.CustomerName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Similarly, code in controller to create entry in database is :
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,RoomNumber,ReservationId")] Confirmed confirmed)
{
if (ModelState.IsValid)
{
db.Confirmeds.Add(confirmed);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.ReservationId = new SelectList(db.Reservations, "ReservationId", "ReservationId", confirmed.ReservationId);
ViewBag.RoomNumber = new SelectList(db.Rooms, "RoomNumber", "RoomType", confirmed.RoomNumber);
return View(confirmed);
}
Can someone help me here to understand, how the selected value of dropdown is passed ?
I want to find reservation Id by using Customer Name for which I have created function like :
// get customer id using customer name that comes from dropdown list
public Customer GetCustomerId(string name)
{
Customer Customer = new Customer();
Customer=db.Customers.Find(name);
return Customer;
}
// use customer Id to get Reservation ID to enter in Reservation table of database
public Reservation getReservationId(int id)
{
Reservation reservation = db.Reservations.Find(id);
return reservation;
}
thanks in advance.
Solution 1:[1]
how the selected value of dropdown is passed ?
You can use asp-items,to new SelectList,it will show it's text and bind it's value to the property you want by ModelBinding.
Below is a work demo, you can refer to it. Custom.cs:
public class Custom
{
public string name{ get; set; }
public int Id { get; set; }
}
CustomViewModel.cs:
public class CustomViewModel
{
public string name { get; set; }
public List<Custom> Customlist { get; set; }
}
In HomeContoller:
public IActionResult Index()
{
var Customlist = new List<Custom>()
{
new Custom { name = "Khushbu",Id= 1 },
new Custom { name = "Mohan", Id = 2 },
new Custom { name = "John", Id = 3 },
new Custom { name = "Martin", Id= 4 },
new Custom { name = "Due", Id= 5 }
};
var model = new CustomViewModel();
model.Customlist = Customlist;
return View(model);
}
[HttpPost]
public IActionResult GetCustomerId(string name)
{
return View();
}
If you are getting data from the database, then you can bind it like below,
model.Customlist= db.Select(x => new Custom { Id = x.Id, Text = x.name }).ToList();
Index view:
@model CustomViewModel
@{
ViewData["Title"] = " List";
}
<div class="text-center">
<h1 class="display-4">Custom Dropdown</h1>
<form asp-action="GetCustomerId" asp-controller="home" method="post">
<div class="row">
<div class="col-md-4">
<select id="drpEmpList" class="form-control" asp-for="name" asp-items="@(new SelectList(Model.Customlist, "name","name" ))">
<option value="">--Select--</option>
</select>
</div>
<div class="col-md-2">
<input type="submit" name="btnSubmit" value="Submit" class="btn btn-success" />
</div>
</div>
</form>
</div>
Result:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Qing Guo |

