ASP.NETMVC更新用户配置文件(实体框架)

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (22)

我是ASP.NET的初学者,我在用户登录后添加“用户更新”功能时遇到问题。

我已经尝试将代码添加到控制器页面,如下所示,但它不起作用。如果有人能给我建议如何解决这些问题,我将不胜感激。

我使用实体框架,下面是我的代码

-CONTROLLER-

public class AccountController : Controller
    {
        TravelExpertsEntities db = new TravelExpertsEntities();
...


 public ActionResult Edit()
        {


            string username = User.Identity.Name;

            // Fetch the userprofile
            Customer user = db.Customers.Where(u => u.CustUserId.Equals(username)).FirstOrDefault();

            // Construct the viewmodel
            Customer model = new Customer();
            model.CustFirstName = user.CustFirstName;
            model.CustLastName = user.CustLastName;
            model.CustEmail = user.CustEmail;
           //...

            return View(model);


        }

        [HttpPost]
        public ActionResult Edit(Customer user)
        {
            using (TravelExpertsEntities db = new TravelExpertsEntities())
            {

                var usr = db.Customers.Where(u => u.CustUserId.Equals(user.CustUserId) && u.CustPassword.Equals(user.CustPassword)).FirstOrDefault();
                if (usr != null)
                {
                    // Update fields
                    user.CustFirstName = usr.CustFirstName;
                    user.CustLastName = usr.CustLastName;
                    user.CustEmail = usr.CustEmail;
                    //...
                    db.Entry(user).State = EntityState.Modified;

                    db.SaveChanges();
                }
                return RedirectToAction("Home", "Home");
            }

            return View(user);
        }
}

-MODEL-
public partial class Customer
    {
        [Key]
        public int CustomerId { get; set; }

        [DisplayName("First Name")]
        [Required(ErrorMessage = "First Name is required")]
        public string CustFirstName { get; set; }

        [DisplayName("Last Name")]
        [Required(ErrorMessage = "Last Name is required")]
        public string CustLastName { get; set; }

        [DisplayName("Address")]
        [Required(ErrorMessage = "Address is required")]
        public string CustAddress { get; set; }
     //...

    }
提问于
用户回答回答于

@MK Kim我的建议是创建一个像这样的ViewModel

`

public class CustomerViewModel
{
    [DisplayName("First Name")]
    [Required(ErrorMessage = "First Name is required")]
    public string CustFirstName { get; set; }
    [DisplayName("Last Name")]
    [Required(ErrorMessage = "Last Name is required")]
    public string CustLastName { get; set; }
    [DisplayName("Address")]
    [Required(ErrorMessage = "Address is required")]
    public string CustAddress { get; set; }
 //...
}

`

所以你可以改变你的控制器看起来像这样

[HttpPost]
    public ActionResult Edit(CustomerViewModel userVm)
    {
        using (TravelExpertsEntities db = new TravelExpertsEntities())
        {

            var usr = db.Customers.Where(u => u.CustUserId.Equals(userVm.CustUserId) && u.CustPassword.Equals(userVm.CustPassword)).FirstOrDefault();
            if (usr != null)
            {
                // Update fields
                usr.CustFirstName =user.CustFirstName;
                usr.CustLastName = user.CustLastName;
                usr.CustEmail = user.CustEmail;
                //...
                db.Entry(usr).State = EntityState.Modified;

                db.SaveChanges();
            }
            return RedirectToAction("Home", "Home");
        }

        return View(user);
    }

所以这意味着您将ViewModel(CustomerViewModel)映射到实际的Model(Customer)。并且不要忘记将新ViewModel传递给View进行模型绑定。

扫码关注云+社区

领取腾讯云代金券

玩转腾讯云 有奖征文活动