获取控制器操作方法中的下拉列表数据

时间:2013-12-13 06:42:07

标签: asp.net-mvc

我的视图中有一个下拉列表我想获取用户在我的控制器操作方法中选择的值。

查看

Specialty Name Active:

 <td>
    <select name="Specialty" id="Specialty"> 
            <option>--Select--</option>  
            <option value="1">True</option>
            <option value="0">False</option>
     </select>
 </td>

型号:

  public class GazelleInfoModel
{
    public GazelleInfo gazelleInfo { get; set; }
    public IList<WCG.Data.EntityObjects.GazelleInfo> ReportModel { get; set; }

}

控制器:

 public ActionResult CreateNewGazelleInfo(GazelleInfoModel gazelleinfoModel, string hdnId, )
            {
                if (!isActive)
                    return LogOut();
                ViewData["CurrentPage"] = "Create New GazelleInfo";
                GazelleInfo gaz = null;
                if (gaz == null)
                {
                    gaz = new GazelleInfo();
                }
                gaz.SpecialtyName = gazelleinfoModel.gazelleInfo.SpecialtyName;
                gaz.SpecialtyNameActive=
                gaz.PreferredLanguage = gazelleinfoModel.gazelleInfo.PreferredLanguage;
                gaz.PreferredLanguageActive =       gazelleinfoModel.gazelleInfo.PreferredLanguageActive;
                gaz.Race = gazelleinfoModel.gazelleInfo.Race;
                gaz.RaceActive = gazelleinfoModel.gazelleInfo.RaceActive;
                gaz.Ethnicity = gazelleinfoModel.gazelleInfo.Ethnicity;
                gaz.EthnicityActive = gazelleinfoModel.gazelleInfo.EthnicityActive;
                gaz.HolidayName = gazelleinfoModel.gazelleInfo.HolidayName;
                gaz.HolidayNameActive = gazelleinfoModel.gazelleInfo.HolidayNameActive;


                GazelleInfoBo.SaveOrUpdate(gaz);

我需要的是将下拉列表中选择的值存储在gaz对象中,以后我将存储在数据库中。

1 个答案:

答案 0 :(得分:0)

选项1

var Specialty = Request.Form["Specialty"];// here request.form is used to get Specialty form variable.



选项2:我建议你使用MVC ModelBinding和Html Helpers。

模型 - 添加新属性专业

控制器 - 获取所有相关专业

 ViewBag.Specialities= new SelectList(Specialities, "Value", "Text");

查看 - 用户强类型html帮助

 @Html.DropDownListFor(x => x.Specialty , new SelectList(ViewBag.Specialities, "Value", "Text", Model.Specialty ))



选项3 - 使用radiobuttons(如果可能)实现真/假逻辑的最佳做法。

@Html.RadioButtonFor(x=>x.Specialty, true)True
@Html.RadioButtonFor(x=>x.Specialty, false)False