Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

CS0116 and CS0103 Error, cant create an IAction?

EDIT: Issue was a buried ‘}’ in my code, apologies for all confusion.

I do not understand why only one specific IAction is returning this error, I have implemented the other actions without any similar error.
I am getting errors of CS0116 and CS0103.
[]
I have defined my viewModel the same manner as other actions, I am attaching the code for the ViewModel and the Detail action below.
The class =>

namespace Paycompute.Controllers

{
    public class EmployeeController : Controller
    {
        private readonly IEmployeeService _employeeService;
        private readonly IWebHostEnvironment _hostingEnvironment;

        public EmployeeController(IEmployeeService employeeService, IWebHostEnvironment hostingEnvironment)

The action =>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    [HttpGet]
public IActionResult Detail(int id)
{
    var employee = _employeeService.GetById(id);
    if (employee == null)
    {
        return NotFound();
    }
    EmployeeDetailViewModel model = new EmployeeDetailViewModel()
    {
        Id = employee.Id,
        EmployeeNo = employee.EmployeeNo,
        ImageUrl = employee.ImageUrl,
        FullName = employee.FullName,
        Gender = employee.Gender,
        Designation = employee.Designation,
        City = employee.City,
        DateJoined = employee.DateJoined,
        SocialSecurityNo = employee.SocialSecurityNo,
        Phone = employee.Phone,
        Email = employee.Email,
        PaymentMethod = employee.PaymentMethod,
        StudentLoan = employee.StudentLoan,
        UnionMember = employee.UnionMember,
        Address = employee.Address,
        City = employee.City,
        ImageUrl = employee.ImageUrl,
        Postcode = employee.Postcode,
    };
    return View(model);
}

The view model =>

using Paycompute.Entity;

namespace Paycompute.Models
{
    public class EmployeeDetailViewModel
    {
        public string FullName { get; set; }
        public int Id { get; set; }
        public string EmployeeNo { get; set; }
        public string Gender { get; set; }
        public string ImageUrl { get; set; }
        public DateTime DOB { get; set; }
        public DateTime DateJoined { get; set; }
        public string Phone { get; set; }
        public string Designation { get; set; }
        public string Email { get; set; }
        public string SocialSecurityNo { get; set; }
        public PaymentMethod PaymentMethod { get; set; }
        public StudentLoan StudentLoan { get; set; }
        public UnionMember UnionMember { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string Postcode { get; set; }
    }
}

EDIT: Upon reviewing my question, I realize from the comments that I have not included the error message, my apologies I am new to the forum.
The messages were:

Error CS0116 A namespace cannot directly contain members such as fields, methods or statements Paycompute C:\Projects\PaycomputeApp\Paycompute\Controllers\EmployeeController.cs 169 Active

Error CS0103 The name ‘_employeeService’ does not exist in the current context Paycompute C:\Projects\PaycomputeApp\Paycompute\Controllers\EmployeeController.cs 171 Active

>Solution :

CS0116 "A namespace cannot directly contain members such as fields or methods." suggests that you’ve forgotten to put your method in a type, i.e.

namespace SomeNamespace 
{
    public class SomeTypeHere // << added - perhaps also : Controller
    {
        [HttpGet]
        public IActionResult Detail(int id)
        {

CS0103 "The name ‘identifier’ does not exist in the current context" is probably related, i.e. because there’s nowhere for NotFound(), View(model) etc to come from.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading