.Net Core Controller编辑ActionResult错误

时间:2019-06-27 15:40:39

标签: asp.net-core

在HttpGet的HttpGet'.Select(user => new AspNetUser()')行上,我无法在此范围中声明名为'user'的本地或参数,因为该名称在封闭的本地范围中用于定义局部或参数'

在HttpPost上,我在Listdata listdata行上收到错误消息,找不到类型或名称空间名称“ Listdata”(是否缺少using指令或程序集引用?)。

    [HttpGet]
      public ActionResult Edit(string id)
      {
        //Get user and return the Edit View
        AspNetUser user = db.Users.Where(u => u.Id == id)
        .Select(user => new AspNetUser()
        {
          Id = user.Id,
          UserName = user.UserName,
          NormalizedUserName = user.NormalizedUserName,
          Email = user.Email,
          NormalizedEmail = user.NormalizedEmail,
          EmailConfirmed = user.EmailConfirmed,
          PasswordHash = user.PasswordHash,
          SecurityStamp = user.SecurityStamp,
          ConcurrencyStamp = user.ConcurrencyStamp,
          PhoneNumber = user.PhoneNumber,
          PhoneNumberConfirmed = user.PhoneNumberConfirmed,
          TwoFactorEnabled = user.TwoFactorEnabled,
          LockoutEnd = user.LockoutEnd,
          LockoutEnabled = user.LockoutEnabled,
          AccessFailedCount = user.AccessFailedCount


          // Add the remainder properties
        }).FirstOrDefault();
          return View(user);
       }


    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult 

   Edit([Bind("Id,Username,NormalizedUserName,Email,NormalizedEmail,
   EmailConfirmed,PasswordHash,SecurityStamp,")] Listdata  listdata)
    {
        if (ModelState.IsValid)
        {
            db.Entry(listdata).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(listdata);
        }

我的期望是,当我单击“编辑”视图页面中的“编辑”链接时,它将转到我要更新其信息并将该更改保存在Post上的用户。

2 个答案:

答案 0 :(得分:0)

出现该错误是因为您在AspNetUser user.Select(user的相同范围内定义了2个用户变量

因此将您的代码更改为此

 AspNetUser userInfo = db.Users.Where(u => u.Id == id) // here
    .Select(user => new AspNetUser()
    {
        Id = user.Id,
        UserName = user.UserName,
        NormalizedUserName = user.NormalizedUserName,
        Email = user.Email,
        NormalizedEmail = user.NormalizedEmail,
        EmailConfirmed = user.EmailConfirmed,
        PasswordHash = user.PasswordHash,
        SecurityStamp = user.SecurityStamp,
        ConcurrencyStamp = user.ConcurrencyStamp,
        PhoneNumber = user.PhoneNumber,
        PhoneNumberConfirmed = user.PhoneNumberConfirmed,
        TwoFactorEnabled = user.TwoFactorEnabled,
        LockoutEnd = user.LockoutEnd,
        LockoutEnabled = user.LockoutEnabled,
        AccessFailedCount = user.AccessFailedCount


        // Add the remainder properties
    }).FirstOrDefault();

    return View(userInfo);

答案 1 :(得分:0)

您在这里两次声明了“用户”-

    AspNetUser user = db.Users.Where(u => u.Id == id)

在这里

    .Select(user => new AspNetUser()
  

在HttpPost上,我在Listdata listdata行上收到错误消息,找不到类型或名称空间名称“ Listdata”(您是否缺少using指令或程序集引用?)。

由于错误状态,您缺少该类的using语句。您需要创建Listdata或添加对其所驻留项目的引用。