表单发布时,将下拉列表中的选定值从视图传递到控制器

本文关键字:视图 控制器 布时 下拉列表 表单 | 更新日期: 2023-09-27 17:57:27

我有这些模型和一个视图模型。当表单过帐以创建条目时,控制器POST方法中WardIDDoctorID的选定值变为零。

查看代码

@model NewClient.HospitalMgt.ViewModel.PatientAdmissionViewModel
.... 
@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <fieldset>
        <div class="editor-label">
            @Html.LabelFor(model => model.Admission.WardID, "Ward")
        </div>
        <div class="editor-field">
            @Html.DropDownList("WardID", String.Empty)
            @Html.ValidationMessageFor(model => model.Admission.WardID)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Admission.DoctorID, "Doctor")
        </div>
        <div class="editor-field">
            @Html.DropDownList("DoctorID", String.Empty)
            @Html.ValidationMessageFor(model => model.Admission.DoctorID)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Patient.PatientName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Patient.PatientName)
            @Html.ValidationMessageFor(model => model.Patient.PatientName)
        </div>
        .... //more controls for properties of Patient
        <div class="editor-label">
            @Html.LabelFor(model => model.Admission.AdmissionNumber)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Admission.AdmissionNumber)
            @Html.ValidationMessageFor(model => model.Admission.AdmissionNumber)
        </div>
        .... //more controls for properties of Admission
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

控制器代码

public ActionResult Create()
{
    ViewBag.WardID = new SelectList(db.Wards, "WardID", "WardNumber");
    ViewBag.DoctorID = new SelectList(db.Doctors, "DoctorID", "DoctorName");
    return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(PatientAdmissionViewModel Id)
{
    if (ModelState.IsValid)
    {
        var c = new Patient()
        {
            Address = Id.Patient.Address,
            ContactNumber = Id.Patient.ContactNumber,
            EmergencyContact = Id.Patient.EmergencyContact,
            PatientName = Id.Patient.PatientName,
        };
        var a = new Admission()
        {
            AdmissionNumber = Id.Admission.AdmissionNumber,
            AdmissionDate = Id.Admission.AdmissionDate,
            **WardID = Id.Admission.WardID, //becomes zero 
            DoctorID = Id.Admission.DoctorID //becomes zero** 
        };
        db.Patients.Add(c);
        db.Admissions.Add(a);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    ViewBag.WardID = new SelectList(db.Wards, "WardID", "WardNumber", Id.Admission.WardID);
    ViewBag.DoctorID = new SelectList(db.Doctors, "DoctorID", "DoctorName", Id.Admission.DoctorID);
    return View(Id);
}

型号和视图型号

//[NotMapped]
public class PatientAdmissionViewModel 
{
    public Patient Patient { get; set; }
    public Admission Admission { get; set; }
}
public class Patient
{
    public int PatientID { get; set; }
    [Required(ErrorMessage = "A Patient Name is required")]
    public string PatientName { get; set; }
    public string Address { get; set; }
    [Required(ErrorMessage = "A Contact Number is required")]
    public string ContactNumber { get; set; }
    public string EmergencyContact { get; set; }
    public virtual Ward Ward { get; set; }
}
public class Admission
{
    public int AdmissionID { get; set; }
    public string AdmissionNumber { get; set; }
    public Nullable<int> PatientID { get; set; }
    public int WardID { get; set; }
    public int DoctorID { get; set; }
    public System.DateTime AdmissionDate { get; set; }
    public virtual Ward Ward { get; set; }
    public virtual ICollection<PatientPayment> PatientPayments { get; set; }
}

表单发布时,将下拉列表中的选定值从视图传递到控制器

使用@Html.DropDownList("WardID", String.Empty)@Html.DropDownList("DoctorID", String.Empty)分别生成具有name="WardID"name="DoctorID"的输入,但建模不包含具有这些名称的属性(但是它包含一个名为Admission的属性,该属性包含这些属性)。

您的"视图模型"实际上不是视图模型(视图模型在编辑时不应包含作为数据模型的属性)。视图模型还应该包含SelectList的属性,以便可以强绑定到模型。您的视图模型需要是

public class PatientAdmissionViewModel
{
    [Display(Name = "Ward"]
    [Required(ErrorMessage = "Please select a ward")]
    public int? SelectedWard { get; set; }
    [Display(Name = "Doctor"]
    [Required(ErrorMessage = "Please select a doctor")]
    public IEnumerable<SelectListItem> WardList { get; set; }
    public int? SelectedDoctor { get; set; }
    public IEnumerable<SelectListItem> DoctorList { get; set; }
    public string PatientName { get; set; }
    ... // other properties of Patient that you need in the view
    public string AdmissionNumber{ get; set; }
    ... // other properties of Admission that you need in the view
}

在GET方法中,初始化视图模型的实例,设置其属性并将其返回到视图

public ActionResult Create()
{
    var model = new PatientAdmissionViewModel()
    {
        WardList = new SelectList(db.Wards, "WardID", "WardNumber"),
        DoctorList = new SelectList(db.Doctors, "DoctorID")
    };
    return View(model);
}

并且在视图中

@Html.DropDownListFor(m => m.SelectedWard, Model.WardList, "-Please select-")
....
@Html.DropDownListFor(m => m.SelectedDoctor, Model.DoctorList, "-Please select-")

最后,在POST方法中,初始化数据模型的新实例,根据视图模型中的值设置它们的属性,并保存数据模型。

我还建议您阅读"MVC中的ViewModel是什么?"的答案?。

而不是此

@Html.DropDownList("WardID", String.Empty)
@Html.DropDownList("DoctorID", String.Empty)

在查看时使用这个

@Html.DropDownListFor(model => model.Admission.WardID, (SelectList)ViewBag.WardID, "--Select One--")
@Html.DropDownListFor(model => model.Admission.DoctorID, (SelectList)ViewBag.DoctorID, "--Select One--")

在控制器上使用这个

 WardID = Id.Admission.WardID, 
 DoctorID = Id.Admission.DoctorID