0

I was Bind one dropdown with some values and call my post action when dropdown selected changed .for look like

@Html.DropDownListFor(m => m.DistrictId, Model.DistrictList, "Select", new
       {
           disableValidation = "true",
           onchange = @"
            var form = document.forms[0]; 
            form.action='Index';
            form.submit();"
       })

This is working fine for call my controller post action . But I can't get dropdown selected value in my model DistrictId property .

For my controller function is look like below

[HttpPost]
        public ActionResult Index(HomeModel homeModel)
        {
            AdminController adminController = new AdminController();
            Guid userId = new Guid();
            homeModel.ComplianceModelList = complianceRepository.LoadComplianceModel(userId, homeModel.DistrictId);
            LoadDistrict(homeModel);
            return View(homeModel);
        }

I want to the dropdown selected DistrictId in my homeModel.DistrictId property .

How to do ?

1
  • Are you sure document.forms[0] is the form that has the DistrictId field? Commented Apr 2, 2013 at 13:57

2 Answers 2

2

Change your Markup like this

@Html.DropDownListFor(m => m.DistrictId, Model.DistrictList, "Select")

and have some javascript to handle your change event so that it will serialize your form and send it to your action method using ajax.

$(function(){
  $("#DistrictId").change(function(e){
    var _this=$(this);

    $.post("@Url.Action("Index","Home")",_this.closest("form").serialize(),
                                                             function(response){
          // do something with response.
    });
  });
});

Assuming you have jQuery library loaded to your page.

Sign up to request clarification or add additional context in comments.

3 Comments

No .It does not call the action .for reason of .Where i give the dropdown id Id in use of this $("#DistrictId")
Check your HTML markup and see what ID is there for the dropdown and use that.
THANKS MAN. there are a lot of examples around that dont work. Your example does!
1

Now I got the solution

Below code was working good .

    @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <h2 class="gridTitle">
            Compliance</h2>
        if (User.Identity.IsAuthenticated && User.IsInRole("SuperAdmin"))
        {
        <div class="complianceSubDiv">
            <div class="complianceRightDiv">
                @Html.LabelFor(model => Model.DistrictId)
                @Html.DropDownListFor(m => m.DistrictId, Model.DistrictList, "Select", new
           {
               disableValidation = "true",
               onchange = @"                        
                form.submit();"
           })</div>
        </div>
        }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.