简体   繁体   中英

How should I add a database record with user field set to currently logged user ASP.NET MVC

I have a problem while adding a category to the logged user. I have an error System.InvalidOperationException: "Sequence has no elements." .

My code:

public class AppUser
{
    public int Id { get; set; }
    public string Nickname { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public DateTime BirthDate { get; set; }
    public virtual ICollection<Category> Categories { get; set; }

}

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
    public AppUser AppUser { get; set; }
    public int AppUserId { get; set; }

}

public class CategoryAndAppUserViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int AppUserId { get; set; }
}

// GET: Categories/Create
public ActionResult Create()
{
    return View();
}

My controller:

// GET: Categories/Create
public ActionResult Create()
{
    return View();
}

// POST: Categories/Create
[Authorize]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(CategoryAndAppUserViewModel category)
{
    if (ModelState.IsValid)
    {

        string userId = User.Identity.GetUserId();
        AppUser appUser = _context.AppUsers.Single(u => u.Name == userId);
        var categoryVM = new Category { Name = category.Name, AppUser = appUser };
        appUser.Categories.Add(categoryVM);
        _context.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(category);
}

I want to do: When the user is logged in, goes to My Examples , create a new Category , fill the new category name, the app must automatically add the new category to the logged in user. The main problem is to get the logged user id.

I have a problem while adding a category to the logged user. I have an error System.InvalidOperationException: "Sequence has no elements." .

My code:

public class AppUser
{
    public int Id { get; set; }
    public string Nickname { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public DateTime BirthDate { get; set; }
    public virtual ICollection<Category> Categories { get; set; }

}

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
    public AppUser AppUser { get; set; }
    public int AppUserId { get; set; }

}

public class CategoryAndAppUserViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int AppUserId { get; set; }
}

// GET: Categories/Create
public ActionResult Create()
{
    return View();
}

My controller:

// GET: Categories/Create
public ActionResult Create()
{
    return View();
}

// POST: Categories/Create
[Authorize]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(CategoryAndAppUserViewModel category)
{
    if (ModelState.IsValid)
    {

        string userId = User.Identity.GetUserId();
        AppUser appUser = _context.AppUsers.Single(u => u.Name == userId);
        var categoryVM = new Category { Name = category.Name, AppUser = appUser };
        appUser.Categories.Add(categoryVM);
        _context.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(category);
}

I want to do: When the user is logged in, goes to My Examples , create a new Category , fill the new category name, the app must automatically add the new category to the logged in user. The main problem is to get the logged user id.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM