Click here to Skip to main content
15,943,623 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
How To Pass Dropdownlist Selected Value from View To Controller in mvc....
Posted
Updated 27-May-19 2:56am
Comments
TweakBird 14-Mar-11 13:13pm    
are tried in google?

if you bind model to view you will get the selected value directly in controller

(without binding the model also we can achieve the selected value from ajax hit)

1.tell me few things have you bind any model to the view in which your dropdown exist.?

2. did you write post action method in controller?


//in view
<% using (Html.BeginForm())
               { %>
            
            <%=Html.DropDownListFor(m => m.CourseId, new SelectList((ViewBag.Courses) as SelectList, "Value", "Text"), "Select")%>
            
            <input id="submit" type="button" value="Save" name="submit" />
            <%} %>

//in document.ready
$("#submit").live("click", function (e) {
                
                e.preventDefault();
                var data = JSON.stringify({ CourseId: $('#CourseId').val()});
                $.ajax({
                    type: "POST",
                    url: config.basePath + '/Controllername/ActionMethodName',
                    cache: false,
                    data: data,
                    dataType: this.dataType,
                    contentType: "application/json; charset=utf-8",
                    success: function (result) {

                        if (result.IsSuccess) {
                            $("#error_message").html(result.ErrorMessage);
                            $("#error_message").addClass("success");
                            UTMSGrid.StudentCourses.List.bindGrid($("#grid"));
                        }
                        else {
                            $("#error_message").html(result.ErrorMessage);
                            $("#error_message").addClass("fail");
                        }
                    },
                    error: function (data) {
                        // $("#error_message").html(data);

                    }
                }); // End ajax call



            });

//in controller
[HttpPost]
        public JsonResult ActionMethodName(ModelName model)
        {
        int id=   model.CourseId //contains selected courseid
            return Json(id, JsonRequestBehavior.AllowGet);
        }
 
Share this answer
 
v3
Comments
indu2012 27-Sep-12 11:41am    
i have 3 drop down in result page .if he select a value from any drop down the result will be filler ?how i do this in mvc3
check this and this. I am sure it will help :)
 
Share this answer
 
Comments
Dalek Dave 18-Mar-11 18:10pm    
Good Links.
)ViewData["REGDNOO"], "--Select--", new { onchange = "document.location.href = 'CategoryDetails/CategoryDetails?id=' + this.options[this.selectedIndex].value;" })%>]]>
 
Share this answer
 
By using hidden field u easily access value in controller
 
Share this answer
 
Hey,

You can also do in this way. Create an view which will be strongly typed.


@model YourClassViewModel
@using (Html.BeginForm())
{
Security :
@Html.DropDownListFor(x => x.SelectedSecurityId ,new SelectList(Model.Securities, "Value", "Text"),"Select one")


CUSP:
@Html.DropDownListFor(x => x.SelectedCUSIPId ,new SelectList(Model.CUSIPs, "Value", "Text"),"Select one")




}

SQL
[HttpPost]
public ActionResult GetThat(YourClassViewModel objVM)
{
   // You can access like objVM.SelectedSecurityId
   //Save or whatever you do please...
}



After submitting an action you will get that selected dropdown value in param object.

--SDK
 
Share this answer
 
@Html.DropDownListFor(p=>p.nmae,(SelectList)ViewBag.Name, "---Select---",
new { id =dlBillStatus", })

this will do...!!!

the thing you hav to pass selected value in this viewbag..!!
 
Share this answer
 
HTML
@Html.DropDownList("ddl", new SelectList((System.Collections.IEnumerable)ViewData["list"], "Id", "Name"))


public ActionResult ActionName(String ddl)
{

  // now ddl has your dropdownlist's selected value i.e Id
 
}


:D
 
Share this answer
 
Comments
CHill60 27-Aug-15 9:12am    
I really hope the OP hasn't waited for over 4 years for this response. Alternatively perhaps they used the solution they accepted
Ishwar Jagtap 2-Jun-16 0:10am    
have any idea how to bind data from one view to another.suppose i have two views in one view i am inserting data in textbox and storing it.so that data i want to be bind in dropdownlist on another view

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900