Solved

How to get the selected value of a DropDownList in MVC

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
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.
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.

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