Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9145109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:25:56+00:00 2026-06-17T10:25:56+00:00

I’m new to MVC and trying to figure out how to set default values

  • 0

I’m new to MVC and trying to figure out how to set default values for partial classes. I have been searching for 2 days now, and can’t get anything to work. Here is a supposed solution, but it doesn’t work for me. I also tried the [DefaultValue(10)] data annotation.

Here is the auto generated partial class created from the edmx file

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

Here is my partial class I created to add annotations.

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

        }

    }
}

And then in my controller, I instantiate a new instance of the class to initialize it, but the values I set don’t get set.

//
        // 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);
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:25:58+00:00Added an answer on June 17, 2026 at 10:25 am

    I found I needed to handle this in my Repository Class in a GetNew() method that would populate the default values of a new instance of the class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have been unable to fix a problem with Java Unicode and encoding. The
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.