?
Solved

How to get the selected value of a DropDownList in MVC

Posted on 2015-01-29
2
Medium Priority
?
3,343 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 21

Accepted Solution

by:
masterpass earned 1600 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 400 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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

777 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