-1

binding of dropdownlist is as follows

@Html.DropDownListFor(model => model.vDeptCode, (SelectList)ViewBag.deptList, "- Select Department -", new { @class = "form-control", @id = "ddl_Dept" })

On the same view i Have add button also after clicking on add button i have to pass deptCode value to my Action for that i have done

function CreateNewVoucher() 
{
    window.location.href = "@Url.Action("Add", "Voucher", new { @vDeptCode =@Html.Raw(Model.vDeptCode) })";
}

but it always pass null value. Please guide me how to pass value to my ActionResult in Controller

vaibhav nemade
  • 11
  • 1
  • 11
  • `@Url.Action()` is razor code which is evaluated on the server before its sent to the view. The route value is not updated just because you change a value in your dropdown. You need to build the url based on the selected value using javascript/jquery –  Apr 04 '16 at 11:08

1 Answers1

0

Look, How i am passing the DropdownList selected value to Controller Post Method.

I have add only necessary Codes here, Please follow this to get your Dropdown value.

Controller

[HttpGet]
public ActionResult Index()
{
    AccountModel account = new AccountModel();
    account.accountStatusList= ObjContext.Status.ToList();
    return View(account);
}

Custom Model Class

public class AccountModel
{
    public int selectedStatusId { get; set; }
    public List<Status> accountStatusList { get; set; }
}

View

@model Nop.Models.AccountModel
@using (Html.BeginForm("Index", "ControllerName", FormMethod.Post))
{
     <div class="row-fluid span12">
          <div class="span3">
             <p><strong>Account Status :</strong></p>
           </div>
           <div class="span5">
             // Bind the dropdown by List.
             @Html.DropDownListFor(model => model.selectedStatusId, new SelectList(Model.accountStatusList, "StatusId", "StatusName"), "--Select--")
           </div>
     </div>
    <div class="row-fluid span12">
          <button class="btn btn-inverse" type="submit" title="Search for Account"><span class="fa fa-search"> Search</span></button>              
    </div>
}

Controller Post Method

[HttpPost]
public ActionResult Index(AccountModel account)
{
    int selectedID = account.selectedStatusId; // You can find your selectedID here..      
    return View(account);
}

See ViewBag vs Model.

Community
  • 1
  • 1
Smit Patel
  • 2,992
  • 1
  • 26
  • 44