model-view-controllerajax.beginform

How to get selected Drop down list value in view part of MVC?


I want to pass selected Drop down list value to Ajax Action Link which is I am using in Controller. Every time When I will change drop down list value. I want that respective value pass to the action link. What I need to write here in Ajax Action Link ????

Drop Down List

<div class="form-group">
        @Html.DropDownListFor(model => model.ComponentId, ((List<string>)ViewBag.Cdll).Select(model => new SelectListItem { Text = model, Value = model }), "  -----Select Id-----  ", new { onchange = "Action(this.value);", @class = "form-control" })
     </div>

Ajax Action Link

 <div data-toggle="collapse">
           @Ajax.ActionLink("Accessory List", "_AccessoryList", new { ComponentId = ???? }, new AjaxOptions()
         {
            HttpMethod = "GET",
            UpdateTargetId = "divacc",
            InsertionMode = InsertionMode.Replace
         })
        </div>

Controller

 public PartialViewResult _AccessoryList(string ComponentId)
  {
     List<ComponentModule> li = new List<ComponentModule>();
     // Code
     return PartialView("_AccessoryList", li);
  }

Solution

  • Here is a new post. I do dropdowns a little different than you, so I am showing you how I do it. When you ask what to pass, I am showing you how to pass the dropdown for 'component' being passed. I also show how to pass from ajax back to the page.

    Controller/Model:

    //You can put this in a model folder
    public class ViewModel
    {
        public ViewModel()
        {
            ComponentList = new List<SelectListItem>();
            SelectListItem sli = new SelectListItem { Text = "component1", Value = "1" };
            SelectListItem sli2 = new SelectListItem { Text = "component2", Value = "2" };
            ComponentList.Add(sli);
            ComponentList.Add(sli2);
        }
    
        public List<SelectListItem> ComponentList { get; set; }
        public int ComponentId { get; set; }
    }
    
    public class PassDDLView
    {
        public string ddlValue { get; set; }
    }
    
    public class HomeController : Controller
    {
        [HttpPost]
        public ActionResult PostDDL(PassDDLView passDDLView)
        {
            //put a breakpoint here to see the ddl value in passDDLView
            ViewModel vm = new ViewModel();
            return Json(new
            {
                Component = "AComponent"
            }
            , @"application/json");
        }
    
        public ActionResult IndexValid8()
        {
            ViewModel vm = new ViewModel();
            return View(vm);
        }
    

    View:

    @model Testy20161006.Controllers.ViewModel
    <!DOCTYPE html>
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>IndexValid8</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $("#btnClick").click(function () {
                    var PassDDLView = { ddlValue: $("#passThis").val() };
                    $.ajax({
                        url: '@Url.Action("PostDDL")',
                        type: 'POST',
                        data: PassDDLView,
                        success: function (result) {
                            alert(result.Component);
                        },
                        error: function (result) {
                            alert('Error');
                        }
                    });
                })
            })
        </script>
    </head>
    <body>
        <div class="form-group">
            @Html.DropDownListFor(m => m.ComponentId,
                         new SelectList(Model.ComponentList, "Value", "Text"), new { id = "passThis" })
            <input type="button" id="btnClick" value="submitToAjax" />
        </div>
    </body>
    </html>