MVC ASP.net中的ViewModel [英] ViewModel in MVC ASP.net

查看:87
本文介绍了MVC ASP.net中的ViewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下拉列表行中遇到异常是我的代码



I am getting exception on dropdownlist line below is my code



@Html .LabelFor(m => m.cities,DCityID,new {@class =control-label col-md-2})


@Html.LabelFor(m => m.cities, "DCityID", new { @class = "control-label col-md-2" })


@ Html.DropDownListFor(m => m.SelectedValue,new SelectList(Model.cities))/////错误


@Html.DropDownListFor(m=> m.SelectedValue, new SelectList(Model.cities))/////error






////////////// viewmodel


//////////////viewmodel

namespace edmxproject2.Models.ViewModels
{
    public class DoctorViewModel
    {
        public int DoctorID { get; set; }
        public string DName { get; set; }
        public int SelectedValue { get; set; }
        public virtual city city { get; set; }

        [DisplayName("City Name")]
        public virtual ICollection<city> cities { get; set; }
    }
}





//////医生班



//////Doctor class

public partial class Doctor
   {
       public Doctor()
       {
           this.Doctor_specialization = new HashSet<Doctor_specialization>();
           this.patients = new HashSet<patient>();
           this.recepionists = new HashSet<recepionist>();
       }

       public int DoctorID { get; set; }
       public string DName { get; set; }
       public Nullable<bool> Dgender { get; set; }
       public Nullable<System.DateTime> DDOB { get; set; }
       public string Demail { get; set; }
       public string DhighSchool { get; set; }
       public string DMedicalCollege { get; set; }
       public string Daddress { get; set; }
       public string DwebLink { get; set; }
       public string Dexperience { get; set; }
       public Nullable<System.DateTime> DgraduationYear { get; set; }
       public Nullable<System.TimeSpan> DinTime { get; set; }
       public Nullable<System.TimeSpan> DoutTime { get; set; }
       [ForeignKey("city")]
       public Nullable<int> DCityID { get; set; }
       public Nullable<int> UserID { get; set; }

       public virtual city city { get; set; }
       public virtual ICollection<Doctor_specialization> Doctor_specialization { get; set; }
       public virtual user user { get; set; }
       public virtual ICollection<patient> patients { get; set; }
       public virtual ICollection<recepionist> recepionists { get; set; }
   }





///// CIty class



/////CIty class

public partial class city
    {
        public city()
        {
            this.Doctors = new HashSet<Doctor>();
            this.patients = new HashSet<patient>();
        }

        public int CityID { get; set; }
        public string cityname { get; set; }

        public virtual ICollection<Doctor> Doctors { get; set; }
        public virtual ICollection<patient> patients { get; set; }

    }

推荐答案

试试这个



Try this

@Html.DropDownListFor(m=> m.SelectedValue, new SelectList(Model.cities,"SelectedValue","Cities"))





希望这可以解决。

否则请检查值是否正确绑定到viewmodel。调试&检查一次



请发表回复/评论



谢谢



hope this works out.
else please check if the values are correctly getting binded to the viewmodel. Debug & check once

Please post reply /comment

Thanks


这篇关于MVC ASP.net中的ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆