为什么我不能将List转换为数组?

时间:2017-07-10 13:21:40

标签: c# arrays asp.net-mvc list casting

在我的项目中我收到错误我不知道为什么我不能将列表转换为类型system.array?

  

System.InvalidCastException:无法转换类型的对象   ' System.Collections.Generic.List`1 [DataLayer.Appoinment]'输入' System.Array'。

public class User: IdentityUser
{

    public User()
    {
       **//when I comment these codes my project is fired.**

        this.Appoinments = new List<Appoinment>();
        this.Offices = new List<Office>();
        this.Schedules = new List<Schedule>();
        this.Secretaries = new List<Secretary>();
    }

    public User(string userName) : base(userName)
    {
    }

    [Display(Name = "نام")]
    [MaxLength(250)]
    [Required(ErrorMessage = " لطفآ {0} را وارد کنید")]
    public string FirstName { get; set; }

    [Display(Name = "نام خانوادگی")]
    [MaxLength(250)]
    [Required(ErrorMessage = " لطفآ {0} را وارد کنید")]
    public string LastName { get; set; }

    [Display(Name = "جنسیت")]
    [Required(ErrorMessage = " لطفآ {0} را وارد کنید")]
    public bool? Gender { get; set; }

    [Display(Name = "تاریخ درج")]
    public DateTime? CreateDate { get; set; }

    [Display(Name = "تاریخ بروزرسانی")]
    public DateTime? UpdateDate { get; set; }

    [Display(Name = "وضعیت")]
    public bool? IsActive { get; set; }

    [MaxLength(500)]
    [Display(Name = "آدرس")]
    public string Address { get; set; }

    [MaxLength(250)]
    [Required(ErrorMessage = " لطفآ {0} را وارد کنید")]
    [Display(Name = "شماره تماس")]
    public virtual IList<Appoinment> Appoinments { get; set; }
    public virtual IList<Office> Offices { get; set; }
    public virtual IList<Schedule> Schedules { get; set; }
    public virtual IList<Secretary> Secretaries { get; set; }
}

public class Appoinment
{
    [Key]          
    public int AppointmentID { get; set; }
    [MaxLength(300)]
    public string TrakingCode { get; set; }
    public DateTime? Shift { get; set; }
    public bool? Status { get; set; }
    public DateTime? Datepick { get; set; }
    public DateTime? CreatedAt { get; set; }
    public DateTime? UpdatedAt { get; set; }
    public virtual User User { get; set; }
    public virtual Schedule Schedule { get; set; }
}

==============================

   @model DataLayer.User

@{
    ViewBag.Title = "مدیریت کاربران";
}

@*<h2>افزودن کاربران</h2>*@

<br />
<div>
    @Html.ActionLink("بازگشت به صفحه اصلی", "Index")
</div>

<!-- /.box -->
@using (Html.BeginForm("Create", "Users", FormMethod.Post))
{
    @Html.AntiForgeryToken();

    <div class="row">
        <div class="col-md-8 col-lg-pull-2">
            <div class="box box-danger">
                <div class="box-header">
                    <h2 class="box-title">افزودن کاربران</h2>
                </div>
                <div class="box-body">

                    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                    <div class="form-group">
                        <label>نام</label>
                        <div class="input-group">
                            <div class="input-group-addon">
                                <i class="fa fa-info"></i>
                            </div>
                            @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control", Type = "text", name = "Firstname", placeholder = "نام" } })
                            @Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
                            @*<input type="text" class="form-control" placeholder="نام " name="FirstName">*@
                        </div>
                        <!-- /.input group -->
                    </div>

                    <div class="form-group">
                        <label>نام خانوادکی</label>
                        <div class="input-group">
                            <div class="input-group-addon">
                                <i class="fa fa-info"></i>
                            </div>
                            @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control", type = "text", name = "LastName", placeholder = "نام خانوادگی" } })
                            @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })

                        </div>
                        <!-- /.input group -->
                    </div>
                    <!-- /.form group -->
                    <!-- phone mask -->
                    <div class="form-group">
                        <label>رمز عبور</label>
                        <div class="input-group">
                            <div class="input-group-addon">
                                <i class="fa fa-key"></i>
                            </div>
                            @Html.EditorFor(model => model.PasswordHash, new { htmlAttributes = new { @class = "form-control", type = "password", name = "Password", placeholder = "...." } })
                            @Html.ValidationMessageFor(model => model.PasswordHash, "", new { @class = "text-danger" })

                        </div>
                        <!-- /.input group -->
                    </div>
                    <!-- /.form group -->
                    <!-- phone mask -->
                    <div class="form-group">
                        <label>ایمیل</label>
                        <div class="input-group">
                            <div class="input-group-addon">
                                <i class="fa fa-envelope"></i>
                            </div>
                            <!-- /.input group -->
                            @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control", type = "email", name = "Password", placeholder = "example@example.com" } })
                            @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })

                        </div>
                        <!-- /.input group -->
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <label>جنسیت</label>
                            <div class="form-group">
                                <div class="radio">

                                    @Html.DropDownListFor(model => model.Gender,
                                       new SelectListItem[] { new SelectListItem() { Text = "مرد", Value = "True" }, new SelectListItem() { Text = "زن", Value = "False" } }
                                       , new { @class = "form-control" })
                                    @Html.ValidationMessageFor(model => model.Gender, "", new { @class = "text-danger" })


                                </div>


                            </div>
                        </div>


                        <div class="col-md-6">
                            <label>وضعیت</label>
                            <div class="form-group">
                                <div class="checkbox">
                                    @Html.DropDownListFor(model => model.Gender,
                                       new SelectListItem[] { new SelectListItem() { Text = "فعال", Value = "True" }, new SelectListItem() { Text = "غیرفعال", Value = "False" } }
                                       , new { @class = "form-control" })
                                    @Html.ValidationMessageFor(model => model.Gender, "", new { @class = "text-danger" })
                                </div>

                            </div>
                        </div>
                    </div>


                    <div class="form-group">
                        <label>شماره همراه</label>
                        <div class="input-group">
                            <div class="input-group-addon">
                                <i class="fa fa-phone"></i>
                            </div>
                            @Html.EditorFor(model => model.PhoneNumber, new { htmlAttributes = new { @class = "form-control", type = "tel", name = "PhoneNumber", placeholder = "شماره همراه..." } })
                            @Html.ValidationMessageFor(model => model.PhoneNumber, "", new { @class = "text-danger" })
                        </div>
                        <!-- /.input group -->
                    </div>


                    <form role="form">
                        <!-- textarea -->
                        <div class="form-group">
                            <label>آدرس</label>
                            @Html.TextAreaFor(model => model.Address, new { @class = "form-control", type = "text", cols = "4", rows = "4", name = "Address", placeholder = "آدرس..." })
                            @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
                            @*<textarea class="form-control" rows="3" placeholder="آدرس ..."></textarea>*@

                        </div>
                    </form>
                    <!-- /.form group -->

                    <div class="row">
                        <div class="col-md-6">
                            <button type="button" class="btn btn-danger btn-flat btn-md"> انصراف</button>
                            <button type="submit" class="btn  btn-success btn-flat btn-md"><span class="fa fa-plus"></span>  افزودن کاربر جدید  </button>
                        </div>
                    </div>

                </div>

                <!-- /.box-body -->
            </div>

        </div>
    </div>

}

1 个答案:

答案 0 :(得分:2)

您在User课程中缺少电话号码属性“شمارهتماس”。