using System;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using Simple_website.Services
namespace simple_website.Controllers
{
public class UserViewModel
{
public Guid Id { get; set; }
public string Login { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Tittle { get; set; }
public string ErrorMessage { get; set; }
public string CreationMode { get; set; }
}
public class UserController
{
private IUserService _userService;
public UserController(IUserService userService)
{
_service = userService;
}
[HttpGet]
public IActionResult Create()
{
ViewBag.Title = "Create a new user";
return View(new UserViewModel());
}
[HttpGet]
public IActionResult Edit(Guid userId)
{
ViewBag.Title = "Edit a user";
var user = _userService.GetUser(userId);
if (user == null)
{
ViewBag.ErrorMessage = $"User '{userId}' was not found!";
return View("Error");
}
var viewModel = new UserViewModel
{
Id = user.Id,
Login = user.Login,
FirstName = user.FirstName,
LastName = user.LastName
};
return View(viewModel);
}
[HttpPost]
public IActionResult Save(UserViewModel viewModel)
{
if (string.IsNullOrEmpty(viewModel.Login))
{
ViewBag.ErrorMessage = "Login is required!";
return View(viewModel);
}
if (ModelState.IsValid)
{
if (viewModel.Id == Guid.Empty) // Creation mode
{
var newUser = new User
{
Login = viewModel.Login,
FirstName = viewModel.FirstName,
LastName = viewModel.LastName
};
_userService.CreateUser(newUser);
}
else // Edit mode
{
var user = _userService.GetUser(viewModel.Id);
if (user == null)
{
ViewBag.ErrorMessage = $"User '{viewModel.Id}' was not found!";
return View("Error");
}
user.Login = viewModel.Login;
user.FirstName = viewModel.FirstName;
user.LastName = viewModel.LastName;
_userService.UpdateUser(user);
}
return RedirectToAction("Confirmation");
}
ViewBag.Title = viewModel.Id == Guid.Empty ? "Create a new user" : "Edit a user";
return View(viewModel);
}
[HttpGet]
public IActionResult Confirmation()
{
return View();
}
}
}
having error of:
the name ViewBag does not exist in the current context.
the name View does not exist in the current context.
the name _userService does not exist in the current context.
userviewModel does not contain definition for firstName
userviewModel does not contain definition for lastName
the name Modelstate does not exist in the current context.
UseViewModel does not contain a definition for FirsrName and no accessible extension ,method FistName accepting a first argument of type UserviewModel could not be found(are you missing a using directive or an assembly reference?)
2
Answers
The error message
usually indicates that you’re trying to use ViewBag outside of a controller action that returns a view. You are using ViewBag in methods like
Create
,Edit
, andSave
, which are action methods. However, you can’t use ViewBag inside[HttpPost]
filter while is not useful, useTempData[]
instead. In addition to that, while using dependency injection your should be using_userService
instead of_service
. Also there is a silly mistake while definingFirsrName
. Hope this helps out 🙂Your Controller class needs to inherit from base
Controller
class.change
public class UserController
topublic class UserController : Controller
and so on.