DropDownList的选定项目

时间:2012-08-07 16:01:10

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

如何获取DropDownList的选定项?

@using (Html.BeginForm("Doctors", "User", FormMethod.Get))
{
    <input name="pageNumber" type="hidden" value="1" /><text>Hospital:</text><br />

    @Html.DropDownList("HospitalId", (IEnumerable<SelectListItem>)ViewBag.HospitalList, new { style = "width:90%" }) <br />

    <button type="submit" class="btn btn-mini"> Search </button>
}

4 个答案:

答案 0 :(得分:0)

这个问题不清楚你想要获取下拉列表项的时间/地点。

我想你现在想要在控制器中拿起它。你需要做的是确保它包含在你的帖子中给控制器,并在表单读取服务器端时使用正确的名称。

以下是一个例子:

@Html.DropDownList("SubworkType", "Select Work Item", new { @Id = "SubworkId" , @Name = "SubworkId"  })

如果您想在视图侧抓取所选值,可以执行以下操作:

var workerType = $("#WorkerTypeFilter option:selected").val()

答案 1 :(得分:0)

它应该只是作为Controller的动作方法中的参数来访问。

public ActionResult Doctors(string pageNumber, string HospitalId)
{
    // make use of HospitalId
    ...
}

答案 2 :(得分:0)

我希望避免使用ViewBag / ViewData之类的动态变量,并坚持使用强类型。

使用ViewModel

public class AssignDoctorViewModel
{
  //Other Properties also
  public IEnumerable<SelectListItem> Hospitals { set;get;}
  public int SelectedHospital { set;get;}
}

在我的GET Action方法中,我将使用填充的属性

返回此值
public ActionResult AssignDoctor
{
  var vm=new AssignDoctorViewModel();
  vm.Hospitals= new[]
    {
          new SelectListItem { Value = "1", Text = "Florance" },
          new SelectListItem { Value = "2", Text = "Spark" },
          new SelectListItem { Value = "3", Text = "Henry Ford" },
    };
    // can replace the above line with loading data from Data access layer.
   return View(vm);
}

现在在您的视图中,我们的ViewModel类是强类型的

@model AssignDoctorViewModel
@using(Html.BeginForm())
{
   @Html.DropDownListFor(x => x.SelectedHospital, Model.Hospitals, "Select..")
  <input type="submit" value="save" />      
}

现在,在您的HTTPPOST操作方法中,您可以通过访问SelectedHospital属性

来获取Selected值
public ActionResult AssignDoctor(AssignDoctorViewModel model)
{ 
  //check for model.SelectedHospital value

}

答案 3 :(得分:0)

Shyju的答案很好,我只想扩展一下Shyju所说的内容。

每个视图使用一个视图模型,只包含您需要的数据,任何未使用的数据请删除。

您的域名模型可能如下所示:

public class Hospital
{
     public int Id { get; set; }

     public string Name { get; set; }
}

您的视图模型可能如下所示:

public class DoctorHospitalViewModel
{
     public int HospitalId { get; set; }

     public IEnumerable<Hospital> Hospitals { get; set; }
}

您的视图可能如下所示(我将下拉列表放在表中以供显示):

<table>
     <tr>
          <td class="edit-label">Hospital <span class="required">**</span></td>
          <td>
               @Html.DropDownListFor(
                    x => x.HospitalId,
                    new SelectList(Model.Hospital, "Id", "Name", Model.HospitalId),
                    "-- Select --"
               )
               @Html.ValidationMessageFor(x => x.HospitalId)
          </td>
     </tr>
</table>

您的控制器可能如下所示,并假设您要在创建视图中使用此下拉列表:

public class HospitalController : Controller
{
     private readonly IHospitalRepository hospitalRepository;

     public HospitalController(IHospitalRepository hospitalRepository)
     {
          // Check that hospitalRepository is not null

          this.hospitalRepository = hospitalRepository;
     }

     public ActionResult Create()
     {
          DoctorHospitalViewModel viewModel = new DoctorHospitalViewModel
          {
               Hospitals = hospitalRepository.GetAll()
          };

          return View(viewModel);
     }

     [HttpPost]
     public ActionResult Create(DoctorHospitalViewModel viewModel)
     {
          // Check that viewModel is not null

          if (!ModelState.IsValid)
          {
               viewModel.Hospitals = hospitalRepository.GetAll();

               return View(viewModel);
          }

          // Do what ever needs to be done
          // You can get the selected hospital id like this
          int selectedHospitalId = viewModel.HospitalId;

          return RedirectToAction("List");
     }
}

我希望这会有所帮助。