-1

如何获得我的 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

4 回答 4

0

它应该可以作为控制器操作方法中的参数简单地访问。

public ActionResult Doctors(string pageNumber, string HospitalId)
{
    // make use of HospitalId
    ...
}
于 2012-08-07T16:28:03.217 回答
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");
     }
}

我希望这有帮助。

于 2012-08-08T07:46:05.513 回答
0

这个问题对于何时/在何处获取下拉列表项不是很清楚。

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

这是一个例子:

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

如果您希望在视图侧获取选定的值,您可以执行以下操作:

var workerType = $("#WorkerTypeFilter option:selected").val()
于 2012-08-07T16:26:22.243 回答
0

我宁愿避免像ViewBag/这样的动态变量ViewData,并坚持使用强类型。

使用视图模型

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

在我的GETAction 方法中,我会用填充的属性返回它

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" />      
}

现在在您的Action 方法中,您可以通过访问PropertyHTTPPOST来获取 Selected 值SelectedHospital

public ActionResult AssignDoctor(AssignDoctorViewModel model)
{ 
  //check for model.SelectedHospital value

}
于 2012-08-07T16:37:59.920 回答