Solved

How to get the selected value of a DropDownList in MVC

Posted on 2015-01-29
2
1,518 Views
Last Modified: 2015-02-15
Hi Expert,

How I can get the selected value of a DropDownList in MVC?

I have code in controller

 [HttpGet]
        public ViewResult Search()
        {

            ViewBag.StudentType = new SelectList(new [] { "Full Time", "Part Time });
            return View();
        }


in the view

<p>Student Type : @Html.DropDownList("Asset Type", (SelectList) ViewBag.StudentType )</p>


How I can get value from user select assign a variable?

Thank you advance.
0
Comment
Question by:dshi15
2 Comments
 
LVL 21

Accepted Solution

by:
masterpass earned 400 total points
ID: 40578921
When the post happens it will post all the form values which will include the "Asset Type".

http://www.codeproject.com/Articles/639709/Getting-Data-From-View-to-Controller-in-MVC  -> is a good starting point.

So if you use: @Html.DropDownList("AssetType", (SelectList) ViewBag.StudentType )
Basically you will have to use Request["AssetType"] to get the value. But if you want it to automatically bind to a model then you will need to use @Html.DropDownListFor and then bind it to the model you have.

http://stackoverflow.com/questions/3057873/how-to-write-a-simple-html-dropdownlistfor will help you get started on dropdownlistfor

Hope this helps.
0
 
LVL 8

Assisted Solution

by:Naitik Gamit
Naitik Gamit earned 100 total points
ID: 40581200
use like below:

@Html.DropDownList("selectedBookId", (SelectList)ViewBag.Books)

[Authorize]
[HttpPost]
public ActionResult Action(string selectedBookId)
{
    if (ValidateFields()
    {
        Book book = FetchYourBookFromTheId(selectedBookId);
        var data = GetDatasAboutBookSelected(book);
        ViewBag.Data = data;
        return View();
    }
 return View();

}
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Using Quotation Marks in PHP This question (http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28217211.html) seems to come up a lot for developers who are new to PHP.  And it got me thinking, "How can we explain the rule…
Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now