-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

この質問は、ドロップダウンリストアイテムをいつ/どこで取得するかについてはあまり明確ではありません。

とりあえずコントローラーで拾いたいと思います。あなたがする必要があるのは、それがコントローラーへのあなたの投稿に含まれていることを確認し、フォームがサーバー側で読み取られるときにピックアップされる正しい名前を使用していることです。

次に例を示します。

@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 メソッドで、プロパティHTTPPOSTにアクセスして Selected 値を取得できます。SelectedHospital

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

}
于 2012-08-07T16:37:59.920 に答える
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 に答える