Solved

How to get the selected value of a DropDownList in MVC

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
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.

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