0

I want to find sum of values in textboxes and then display the SUM in another textbox and also STORE THE SUM IN DATABASE . Using the code below it succesfully find sum and display it in another textbox However it does not store the sum in database.

I checked the database, it shows the value NULL is entered in TotalAvgHours field of database.

[HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Create(TimePeriodViewModel timePeriodVM)
            {
                var timeperiod = new TimePeriod
                {
                 
                    TotalAvgHours = timePeriodVM.TimePeriod.TotalAvgHours,
                    
    }

View

Total
            <td>
        @Html.TextBox("TotalAvgHours", "", "{0:c}", new { disabled = "disabled", @class = "form-control", id = "res2" })
        <script type="text/javascript">

            function sum2() {
                var one = document.getElementById('a2').value || "0";
                var two = document.getElementById('b2').value || "0";
                var three = document.getElementById('c2').value || "0";
                var four = document.getElementById('d2').value || "0";
                var five = document.getElementById('e2').value || "0";
                var six = document.getElementById('f2').value || "0";
                var seven = document.getElementById('g2').value || "0";
                var eight = document.getElementById('h2').value || "0";
                var nine = document.getElementById('i2').value || "0";
                var ten = document.getElementById('j2').value || "0";
                var eleven = document.getElementById('k2').value || "0";
                var twelve = document.getElementById('l2').value || "0";
                var thirteen = document.getElementById('m2').value || "0";
                var fourteen = document.getElementById('n2').value || "0";
                var fifteen = document.getElementById('o2').value || "0";

                var result = parseFloat(one) + parseFloat(two) + parseFloat(three) + parseFloat(four) + parseFloat(five) + parseFloat(six) + parseFloat(seven) +
                    parseFloat(eight) + parseFloat(nine) + parseFloat(ten) + parseFloat(eleven) + parseFloat(twelve) + parseFloat(thirteen) + parseFloat(fourteen) + parseFloat(fifteen);
                if (!isNaN(result)) {
                    document.getElementById('res2').value = result;
                }
            }
            window.onload = sum2;

        </script>
    </td>
    
    
</tr>

TimePeriodViewModel

 public class TimePeriodViewModel
    {
        public IEnumerable<Personnel> Personnels { get; set; }
       //public Personnel Personnel { get; set; }
       public int PersonnelID { get; set; }
       
        public IEnumerable<DailyRegister> DailyRegisters { get; set; }
        public DailyRegister DailyRegister { get; set; }
        public TimePeriod TimePeriod { get; set; }

    }

TimePeriod

 public partial class TimePeriod
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public TimePeriod()
        {
            this.DailyRegisters = new HashSet<DailyRegister>();
        }
    
        public int TimePeriodID { get; set; }
        public int PersonnelID { get; set; }
        public System.DateTime StartDate { get; set; }
        public System.DateTime EndDate { get; set; }
        public Nullable<bool> IsSignedAvgAgreement { get; set; }
        public Nullable<bool> IsSignedTimeSheet { get; set; }
        public string SignedAvgBy { get; set; }
        public string SignedTimeSheetBy { get; set; }
        public Nullable<System.DateTime> SignedTimeSheetDate { get; set; }
        public Nullable<decimal> TotalAvgHours { get; set; }
        public Nullable<decimal> TotalTimeSheetHours { get; set; }
        public Nullable<decimal> TotalOvertime { get; set; }
}
3
  • Did you debug the request? We are not seeing any dbContext operation the code you shared so far? Did you shared the full code? In addition please share TimePeriodViewModel class details as well. Commented Mar 1, 2022 at 6:33
  • @MdFaridUddinKiron Please see updated question Commented Mar 1, 2022 at 7:03
  • When you debug your Create method, does timePeriodVM.TimePeriod.TotalAvgHours have the expected value? If it has, do you really update the database (you don't show that code)? Commented Mar 1, 2022 at 7:41

1 Answer 1

1

Due to disable attribute property applied on you are textbox you are not able to fetch data of it on post instead of it use readonly attribute property on textbox.

Try :

 @Html.TextBox("TotalAvgHours", "", "{0:c}", new { @readonly = "readonly", @class = "form-control", id = "res2" })
        
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.