2012-08-07 173 views
-1

如何獲取我的DropDownList的選定項目?已選擇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> 
} 
+0

請參閱我的DDL教程http://www.asp。net/mvc/tutorials/javascript/working-with-the-dropdownlist-box-and-jquery/using-the-dropdownlist-helper-with-aspnet-mvc和http://blogs.msdn.com/b/rickandy/存檔/ 2012/01/09/cascasding-下拉列表,在-ASP淨mvc.aspx – RickAndMSFT 2012-08-07 18:16:09

回答

0

這個問題不是很清楚你想要什麼時候下拉列表項目。

我會假設你現在想要在控制器中選擇它。你需要做的是確保它在你的文章中包含到控制器中,並且在表單被讀取到服務器端時使用正確的名字。

下面是一個例子:

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

如果您正在尋找搶在視圖上側所選擇的值,你可以做這樣的事情:

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

應該僅僅是作爲訪問參數在您的Controller的操作方法中。

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

我寧願避免動態變量,如ViewBag/ViewData並具有很強的類型堅守。

使用視圖模型

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

在我GET行動的方法,我會用充滿性

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 Action me的ThOD,您可以通過訪問SelectedHospital物業

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

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

你的控制器可能看起來像這樣,並假設你想要使用th是創建視圖中的下拉列表:

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"); 
    } 
} 

我希望這有助於。

相關問題