1

私はMVCを初めて使用し、部分クラスのデフォルト値を設定する方法を理解しようとしています。私は今2日間検索していますが、何も動作しません。これが想定される解決策ですが、私にはうまくいきません。[DefaultValue(10)]データ注釈も試しました。

これは、edmxファイルから作成された自動生成された部分クラスです。

//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace OTIS.Models.Admin
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;

    public partial class Company
    {
        public Company()
        {
            this.Facilities = new HashSet<Facility>();
        }

        public int CompanyID { get; set; }

        [Required]
        [Display(Name = "Company Name")]
        public string CompanyName { get; set; }

        public string Address1 { get; set; }
        public string Address2 { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string PostalCode { get; set; }
        public decimal ItemMargin { get; set; }
        public decimal ServicesMargin { get; set; }
        public decimal InvoiceTimeIncrement { get; set; }
        public decimal CashDiscountPct { get; set; }
        public decimal BaseServiceHourlyRate { get; set; }
        public decimal HourlyPremiumRush { get; set; }
        public decimal HourlyPremiumLate { get; set; }
        public decimal HourlyPremiumCustomerMaterial { get; set; }
        public int CreatedByID { get; set; }
        public System.DateTime CreatedOn { get; set; }
        public int ModifiedBy { get; set; }
        public System.DateTime ModifiedOn { get; set; }

        public virtual UserProfile UserProfile { get; set; }
        public virtual UserProfile UserProfile1 { get; set; }
        public virtual ICollection<Facility> Facilities { get; set; }
    }
}

これが、アノテーションを追加するために作成した部分クラスです。

namespace OTIS.Models.Admin
{
    [MetadataType(typeof(CompanyMD))]
    public partial class Company
    {

        //public Company()
        //{
        //    //private System.DateTime _currentDateTime = DateTime.Now;
        //    ////Set Default Values
        //    //CreatedByID = (int)Membership.GetUser().ProviderUserKey;
        //    //CreatedOn = _currentDateTime;
        //    //ModifiedBy = (int)Membership.GetUser().ProviderUserKey;
        //    //ModifiedOn = _currentDateTime;
        //}

        public string FullAddress
        {
            get
            {
                return this.City + ", " + this.State + " " + this.PostalCode;
            }
        }

        public class CompanyMD
        {
            private System.DateTime _currentDateTime = DateTime.Now;
            private int _currentUser = (int)Membership.GetUser().ProviderUserKey;


            [Display(Name = "Company ID")]
            public int CompanyID { get; set; }

            [Required]
            [Display(Name = "Company Name")]
            public string CompanyName { get; set; }

            [Display(Name = "Address")]
            public string Address1 { get; set; }

            [Display(Name = "Address 2")]
            public string Address2 { get; set; }

            public string City { get; set; }
            public string State { get; set; }

            [Display(Name = "Zip")]
            public string PostalCode { get; set; }

            [Display(Name = "Address")]
            public string FullAddress { get; set; }

            [Display(Name = "Material Margin")]
            public decimal ItemMargin { get; set; }

            [Display(Name = "Overtime Margin")]
            public decimal ServicesMargin { get; set; }

            [Display(Name = "Invoice Hour Increment")]
            public decimal InvoiceTimeIncrement { get; set; }

            private decimal _cashDiscountPct;

            [Display(Name = "Cash Discount %")]
            [DisplayFormat(DataFormatString = "{0:P2}")]
            public decimal CashDiscountPct
            {
                get { return _cashDiscountPct; }
                set { _cashDiscountPct = value/100; }
            }

            [Display(Name = "Base Hourly Rate ($/Hr)")]
            [DataType(DataType.Currency), DisplayFormat(DataFormatString = "{0:C2}", ApplyFormatInEditMode = true)]
            public decimal BaseServiceHourlyRate { get; set; }

            [Display(Name = "Rush Premium ($/Hr)")]
            [DataType(DataType.Currency), DisplayFormat(DataFormatString = "{0:C2}", ApplyFormatInEditMode = true)]
            public decimal HourlyPremiumRush { get; set; }

            [Display(Name = "Late Premium ($/Hr)")]
            [DataType(DataType.Currency), DisplayFormat(DataFormatString = "{0:C2}", ApplyFormatInEditMode = true)]
            [DefaultValue(75)]
            public decimal HourlyPremiumLate { get; set; }

            [Display(Name = "Cust Material Premium ($/Hr)")]
            [DataType(DataType.Currency), DisplayFormat(DataFormatString = "{0:C2}", ApplyFormatInEditMode = true)]
            public decimal HourlyPremiumCustomerMaterial { get; set; }

            [Display(Name = "Created By")]
            public int CreatedByID { get; set; }
            //{
            //    get { return _currentUser; }
            //    set { _currentUser = value; }
            //}

            [Display(Name = "Created On")]
            [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
            //[DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
            public System.DateTime CreatedOn 
            {
                get { return _currentDateTime; }
                set { _currentDateTime = value; }
            }

            [Display(Name = "Modified By")]
            public int ModifiedBy { get; set; }
            //{
            //    get { return _currentUser; }
            //    set { _currentUser = value; }
            //}

            [Display(Name = "Modified On")]
            public System.DateTime ModifiedOn
            {
                get { return _currentDateTime; }
                set { _currentDateTime = value; }
            }

        }

    }
}

次に、コントローラーで、クラスの新しいインスタンスをインスタンス化して初期化しますが、設定した値が設定されません。

//
        // GET: /Company/Create

        public ActionResult Create()
        {
            ViewBag.CreatedByID = new SelectList(db.UserProfiles, "UserId", "UserName");
            ViewBag.ModifiedBy = new SelectList(db.UserProfiles, "UserId", "UserName");
            Company newCompany = new Company();
            return View(newCompany);
        }
4

2 に答える 2

3

申し訳ありませんが、これは遅すぎますが、私は自分で同様のシナリオを解決しました。

問題は、部分クラスをどのように参照するかということだと思います。これは、コードのない部分クラスへの空の参照である必要があります。EFはこの「宣言」を使用して、部分クラスをメタデータクラスにリンクします。したがって、メタデータクラスは次のようになります。

namespace OTIS.Models.Admin
{
    [MetadataType(typeof(CompanyMD))]
    public partial class Company
    {}  // <-- note the close bracket!

    public class CompanyMD
    {
        private System.DateTime _currentDateTime = DateTime.Now;
        private int _currentUser = (int)Membership.GetUser().ProviderUserKey;

        public string FullAddress
        {
            get
            {
                return this.City + ", " + this.State + " " + this.PostalCode;
            }
        }

        [Display(Name = "Company ID")]
        public int CompanyID { get; set; }
        [Required]
        [Display(Name = "Company Name")]
        public string CompanyName { get; set; }

        // ....etc.... removed for brevity

    } // close metadata class
} // close namespace

お役に立てれば!

于 2013-01-15T20:11:42.753 に答える
0

クラスの新しいインスタンスのデフォルト値を設定するGetNew()メソッドのリポジトリクラスでこれを処理する必要があることがわかりました。

于 2013-01-16T21:56:20.137 に答える