Solved

How to get the selected value of a DropDownList in MVC

Posted on 2015-01-29
2
2,614 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 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

730 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