How to get DropDownList SelectedValue in Controller in MVC
1st Approach (via Request or FormCollection):
You can read it from Request
using Request.Form
, your dropdown name is ddlVendor
so pass ddlVendor
key in the formCollection to get its value that is posted by form:
string strDDLValue = Request.Form["ddlVendor"].ToString();
or Use FormCollection
:
[HttpPost]
public ActionResult ShowAllMobileDetails(MobileViewModel MV,FormCollection form)
{
string strDDLValue = form["ddlVendor"].ToString();
return View(MV);
}
2nd Approach (Via Model):
If you want with Model binding then add a property in Model:
public class MobileViewModel
{
public List<tbInsertMobile> MobileList;
public SelectList Vendor { get; set; }
public string SelectedVendor {get;set;}
}
and in View:
@Html.DropDownListFor(m=>m.SelectedVendor , Model.Vendor, "Select Manufacurer")
and in Action:
[HttpPost]
public ActionResult ShowAllMobileDetails(MobileViewModel MV)
{
string SelectedValue = MV.SelectedVendor;
return View(MV);
}
UPDATE:
If you want to post the text of selected item as well, you have to add a hidden field and on drop down selection change set selected item text in the hidden field:
public class MobileViewModel
{
public List<tbInsertMobile> MobileList;
public SelectList Vendor { get; set; }
public string SelectVendor {get;set;}
public string SelectedvendorText { get; set; }
}
use jquery to set hidden field:
<script type="text/javascript">
$(function(){
$("#SelectedVendor").on("change", function {
$("#SelectedvendorText").val($(this).text());
});
});
</script>
@Html.DropDownListFor(m=>m.SelectedVendor , Model.Vendor, "Select Manufacurer")
@Html.HiddenFor(m=>m.SelectedvendorText)
How to get DropDownList Selected Value in MVC
Your view model should be the class which represents the properties needed by your view. So if you want to add a dropdown to your view, add 2 more properties to your viewmodel, one for the list of items needed to build the select options and another for the selected option value.
public class ResourceViewModel
{
public int DeveloperTypeId { set;get;}
public List<SelectListItem> DeveloperTypes { set;get;}
public string FirstName {get; set;}
public string LastName {get; set;}
}
Now in your GET action, create an object of this view model, initialize the DeveloperTypes
collection property and send the object to the view.
public ActionResult NewUser()
{
var vm = new ResourceViewModel();
vm.DeveloperTypes = db_RIRO.sp_GetAllDeveloperType()
.Select(a=> new SelectListItem {
Value = a.DeveloperTypeID.ToString(),
Text= a.Developer_Type })
.ToList();
return View(vm);
}
[HttpPost]
public void AddUser(ResourceViewModel model)
{
//check model.DeveloperTypeId
// to do : Return something
}
Assuming db_RIRO.sp_GetAllDeveloperType()
returns a collection of objects with a DeveloperTypeID
property of int
type and a Developer_Type
of string
type.
Now in your view, you can use DropDownListFor
helper
@model ResourceViewModel
@using(Html.BeginForm("AddUser","Resource"))
{
@Html.LabelFor(a=>a.FirstName)
@Html.TextBoxFor(a=>a.FirstName)
@Html.LabelFor(a=>a.LastName)
@Html.TextBoxFor(a=>a.LastName)
@Html.LabelFor(a=>a.DeveloperTypeId)
@Html.DropDownListFor(a=>a.DeveloperTypeId, Model.DeveloperTypes,"Select");
<button type="submit" class="btn">Submit</button>
}
How to get DropDownList SelectedValue in Controller in MVC and store it in a variable
You should use @Html.DropDownListFor
for select1
.
This will bind it to a property in your ViewModel.
For example,
@Html.DropDownListFor(m => m.select1, m.SelectListOptions)
The ViewModel will be available to your Controller methods.
In order to populate the Options for your dropdown, you will need to create a SelectList
and populate it with SelectListItems
.
How to get DropDownList selected Text in MVC controller
new SelectList(Model.dropConfig, "Project", "Project")
The above change solves my problem. Thanks @StephenMuecke :)
how to pass dropdownlist selected value to get function in controller in mvc
Try this:
var jsn = { ProductID: SelectedProductID };
jsn = JSON.stringify(jsn);
$.ajax({
type: "POST",
url: "/Inventory/ViewProduct",
data: jsn,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function () {
}
});
pass the selected value of Html.DropDownList to controller
Try to use DropDownListFor
@model PatientInfo
....
@{
var items= new List<SelectListItem>()
{
new SelectListItem() { Text= "son", Value = "son" },
new SelectListItem() { Text= "daughter", Value = "daughter" }
};
}
@using (Html.BeginForm()) {
@Html.DropDownListFor (model=>model.relationshipWithPatient, items , "relationship...", new { @class = "form-control" })
}
or I prefer
<select class="form-control" asp-for="@Model.relationshipWithPatient" asp-items="@items"></select>
action
public ActionResult Create(PatientInfo vdm)
{
if (ModelState.IsValid)
{
var selected= vdm.relationshipWithPatient; //selected value of RelationshipWithFather
.....
}
}
Get the selected value of a DropDownList. Asp.NET MVC
In HTML a dropdown box sends only simple scalar values. In your case that would be the id of the selected book:
@Html.DropDownList("selectedBookId", (SelectList)ViewBag.Books)
and then adapt your controller action so that you will retrieve the book from the id that gets passed to your controller action:
[Authorize]
[HttpPost]
public ActionResult Action(string selectedBookId)
{
if (ValidateFields()
{
Book book = FetchYourBookFromTheId(selectedBookId);
var data = GetDatasAboutBookSelected(book);
ViewBag.Data = data;
return View();
}
return View();
}
Related Topics
Fire and Forget Async Method in ASP.NET MVC
Where Is the Application.Doevents() in Wpf
Add New Item in Existing Array in C#.Net
How to Change Menu Hover Color
How to Serialize a Dictionary as Part of Its Parent Object Using JSON.Net
Order of Linq Extension Methods Does Not Affect Performance
Passing Values Between Windows Forms C#
Reading PDF Content Using Itextsharp in C#
Best Practices for Exception Management in Java or C#
Getting Value from Appsettings.JSON in .Net Core
List of Timezone Ids for Use with Findtimezonebyid() in C#
Differencebetween Task.Run() and Task.Factory.Startnew()
Entity Framework 4 - Addobject VS Attach
How to Create a Self-Signed Certificate Using C#
Getting Image Dimensions Without Reading the Entire File