C# 表单发布时,将dropdownlist中的选定值从视图传递给控制器

C# 表单发布时,将dropdownlist中的选定值从视图传递给控制器,c#,asp.net-mvc-4,C#,Asp.net Mvc 4,我有这些模型和一个视图模型。当表单过帐以创建条目时,控制器过帐方法中WardID和DoctorID的选定值变为零 视图代码 @model NewClient.HospitalMgt.ViewModel.PatientAdmissionViewModel .... @using (Html.BeginForm()) { @Html.AntiForgeryToken() @Html.ValidationSummary(true) <fieldset>

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

视图代码

@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>
}
模型和视图模型

//[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; }
}
/[notmap]
公共类PatientMissionViewModel
{
公共患者{get;set;}
公开入场{get;set;}
}
公立病人
{
public int PatientID{get;set;}
[必需(ErrorMessage=“需要患者姓名”)]
公共字符串PatientName{get;set;}
公共字符串地址{get;set;}
[必需(ErrorMessage=“需要联系电话”)]
公共字符串ContactNumber{get;set;}
公共字符串紧急联系人{get;set;}
公共虚拟病房{get;set;}
}
公开课入学
{
public int-AdmissionID{get;set;}
公共字符串许可号{get;set;}
公共可空PatientID{get;set;}
public int-WardID{get;set;}
public int DoctorID{get;set;}
public System.DateTime许可日期{get;set;}
公共虚拟病房{get;set;}
公共虚拟ICollection PatientPayments{get;set;}
}

您使用的
@Html.DropDownList(“WardID”,String.Empty)
@Html.DropDownList(“DoctorID”,String.Empty)
分别使用
name=“WardID”
name=“DoctorID”
生成输入,但您的模型不包含具有这些名称的属性(但是,它确实包含一个名为
acmission
的属性,该属性包含这些属性)

您的“视图模型”实际上不是视图模型(视图模型不应包含编辑时为数据模型的属性)。视图模型还应包含
选择列表的属性,以便您可以强绑定到模型。您的视图模型需要

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
}
在我看来

@Html.DropDownListFor(m => m.SelectedWard, Model.WardList, "-Please select-")
....
@Html.DropDownListFor(m => m.SelectedDoctor, Model.DoctorList, "-Please select-")
最后,在POST方法中,初始化数据模型的新实例,根据视图模型中的值设置它们的属性,然后保存数据模型

我还建议您阅读。

的答案,而不是此答案

@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 

我也将尝试此方法,专家们提供的巨大帮助使编写验证代码变得有趣,并且属性的顺序必须以其他方式匹配具有键“”的ViewData项的类型为System.Int32,但必须是类型为“IEnumerable error comes”`public IEnumerable WardList{get;set;}[Required(ErrorMessage=“请选择一个病房”)]public int?SelectedWard{get;set;}public IEnumerable doctor list{get;set;}[必需(ErrorMessage=“请选择医生”)]public int?SelectedDoctor{get;set;}`您试图对我的答案进行编辑。该编辑错误,我拒绝了。如果您在发布时遇到错误,请参阅
@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