从MVC中减去BD中的值

时间:2019-06-04 12:52:15

标签: asp.net-mvc

我有一个MVC索引可以从存储过程中列出我的数据库数据,并且有1列名为“小时”,因此在编辑表单中,我想从数据库中减去插入的值。 我该如何在索引中显示? 谢谢

这是我的模特

 public class ExtrajobsViewModel
    {
        public int ExtraJobsID { get; set; }
        public int UserID { get; set; }
        public int FunctionScopeID { get; set; }
        public string Description { get; set; }
        [DisplayFormat(DataFormatString = "{0:dd/MMM/yyyy}")]
        public Nullable<System.DateTime> Day { get; set; }
        public Nullable<decimal> Hours { get; set; }

和我的编辑modalview的控制器:

public ActionResult _CreateOrEdit_IndexRegistoTC(int? id)
        {

            if (id == null || id == 0)
            {
                return View();
            }
            var FunctionName = _context.TBL_UserFunction.Where(u => u.Name == "Extra-jobs").FirstOrDefault().Name;
            var item2 = _context.SP_ExtraJobsRel(FunctionName).Where(m => m.ExtraJobsID == id).FirstOrDefault();

            var item = _context.TBL_ExtraJobs.Find(id);

            if (item2 == null)
            {

                return HttpNotFound();
            }

            ExtrajobsViewModel model = new ExtrajobsViewModel();
            model.ExtraJobsID = item.ExtraJobsID;
            model.FunctionScopeID = item.FunctionScopeID;
            model.Travel_Consultant = item2.Username;
            model.Scope1UserFunction = item2.functionscope1;
            model.Scope2UserFunctionScope = item2.functionscope2;
            model.Description = item2.Description;
            model.Day = DateTime.UtcNow;
            model.Hours = item2.Hours;
            return View(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult _CreateOrEdit_IndexRegistoTC(ExtrajobsViewModel model)
        {

            var userID = ((SessionModel)Session["SessionModel"]).UserID; // get current user id
            TBL_ExtraJobs item = _context.TBL_ExtraJobs.Find(model.ExtraJobsID);
            _context.Entry(item).State = EntityState.Modified;
            item.FunctionScopeID = model.FunctionScopeID;
            item.Description = model.Description;
            item.Hours = model.Hours;
            item.Day = model.Day;
            item.LastUpdate = DateTime.UtcNow;
            item.LastUpdateBy = userID;
            //GetViewBags(model.Status_Coordinator);
            _context.SaveChanges();
            return RedirectToAction("IndexValidacao");
        }

我的视图显示小时数:

<div class="form-group">
                @Html.LabelFor(m => m.Hours, new { @class = "control-label col-sm-3" })
                <div class="col-sm-9">
                    @Html.EditorFor(model => model.Hours, new { htmlAttributes = new { @class = "form-control input" } })
                    @Html.ValidationMessageFor(model => model.Hours, "", new { @class = "text-danger" })
                </div>
            </div>

0 个答案:

没有答案