Pass the selected value from the drop-down list from the view to the controller when the form message

I have these models and presentation model. When creating a form for creating records, the selected value for WardIDand DoctorIDbecomes equal to zero in the controller's POST method.

View code

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

Controller code

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

Models and View Models

//[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; }
}
+4
source share
2 answers

You use @Html.DropDownList("WardID", String.Empty)and @Html.DropDownList("DoctorID", String.Empty), generating inputs with name="WardID"and, name="DoctorID"respectively, but your model does not contain properties with these names (however, it contains a property with a name Admissionthat contains those properties).

' ( , ). 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 , .

ViewModel MVC?.

+2

@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 
0

Source: https://habr.com/ru/post/1648198/


All Articles