如何在回发后获得下拉值

时间:2012-05-05 20:49:10

标签: c# asp.net-mvc-3 drop-down-menu http-post viewmodel

我有一个带有下拉列表的视图,它与视图模型相关联...

视图模型的一个属性:

public int selectDTypeID { get; set; }

视图有一个下拉列表:

   Html.DropDownListFor(
       model => model.selectDTypeID , 
       new SelectList(
              new List<Object>{ 
                   new { value = 1 , text = "Test1"  },
                   new { value = 2 , text = "Test2" },
                   new { value = 3 , text = "Test3"}
                },
              "value",
              "text"
       )
    )

如何在不使用额外参数的情况下获取回帖下拉列表的值...我只是希望能够这样做...

[HttpPost]
public ActionResult ContinueDonation(AddDonationViewModel model)
{
 var id = model.selectDTypeID;

}

我可以这样做吗?

1 个答案:

答案 0 :(得分:2)

如果您的视图是这样的强类型视图,

@model AddDonationViewModel
@using (Html.BeginForm())
{
   @Html.DropDownListFor(
       model => model.selectDTypeID , 
       new SelectList(
              new List<Object>{ 
                   new { value = 1 , text = "Test1"  },
                   new { value = 2 , text = "Test2" },
                   new { value = 3 , text = "Test3"}
                },
              "value",  "text"))    

    <input type="submit" value="go" />
}

单击“提交”时,HttpPost操作方法中的SelectdTYpeID属性中将显示所选值

[HttpPost]
public ActionResult ContinueDonation(AddDonationViewModel model)
{
  var id = model.selectDTypeID;  // You have your selected ID here
}