从数据库中获取数据并拉入视图

时间:2019-06-13 10:10:51

标签: c# sql asp.net-mvc model-view-controller

我想对代码进行一些修改。该代码是关于更新用户详细信息的,该详细信息已存在于数据库中,用户可以在其中更新他的名字和姓氏。

我已将动作结果编码为updateName。 我已经实现了一个使用Ajax的按钮,并为名字和姓氏使用了2个标签。 但是我很困惑如何在视图中实现它。

 [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult UpdateName(TeacherUpdateInfo updateInfo)
            {

                if (ModelState.IsValid)
                {
                    try
                    {
                        string id = User.Identity.GetUserId();
                        var t = db.Teacher.AsNoTracking().Where(b => b.UserId == id).FirstOrDefault();

                        return Json("Success");
                    }
                    catch (Exception e)
                    {
                        LogManager.HandleUIException(User.Identity.Name, e, ErrorSeverity.Level1);
                        return Json("Something went wrong");
                    }
                }
                return Json("Model State Invalid");
            }
            [HttpGet]
            public ActionResult TeacherUpdateName()
            {
                TestimonialsViewModel model = new TestimonialsViewModel();

                model.Init(db);

                return View(model);
            }

在另一个控制器中,我的编码为:

[HttpGet]
        public ActionResult UpdateName()
        {
            try
            {
                Teacher t = null;
                t = FindTeacher(ViewBag.UserEntityId, User);

                if (t == null)
                {
                    // if somehow they are in the app without a known role assigned kick them out
                    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
                    return RedirectToAction("SignIn", "Account");
                }

                TeacherUpdateNameViewModel model = new TeacherUpdateNameViewModel();

                model.Init(db, t);

                return View("TeacherUpdateName");
            }
            catch (Exception ex)
            {
                return View("Error", LogManager.HandleUIException(User.Identity.Name, ex, ErrorSeverity.Level1));
            }

        }

我希望当用户更新他的名字时,应该给他以前的名字,那个先前的名字必须从数据库中获取。 但以上内容并未反映在网页中。 请帮助我

0 个答案:

没有答案