如何将dropdownlist选择值传递给控制器

时间:2016-04-04 10:40:50

标签: asp.net-mvc-3

下拉列表的绑定如下

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

在同一个视图上我也点击了添加按钮后添加按钮我必须将deptCode值传递给我已经完成的动作

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

但它总是传递null值。 请指导我如何将值传递给Controller中的ActionResult

1 个答案:

答案 0 :(得分:0)

看,我如何将DropdownList选中的值传递给Controller Post Method。

我在这里只添加了必要的代码,请按照此处获取您的Dropdown值。

<强>控制器

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

自定义模型类

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

查看

@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>
}

控制器发布方法

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

请参阅ViewBag vs Model