Home > Net >  data comes to controller but not to view
data comes to controller but not to view

Time:12-22

 public ActionResult Index()
        {
         
        GetEmployee();

        return View();
    }

    private void GetEmployee()
    {
       
        SocialMedia item = new SocialMedia();
        var employee = _employeeRepository.GetById(Session.GetEmployeeNo());           
           //employee.No, employee.ManagerNo
            item.FirstName = employee.FirstName;
            item.LastName = employee.LastName;
            item.Departmen = employee.PositionCode;
        item.FullName = item.FirstName   " "   item.LastName;
        
    }

And my HTML

@using Models.Model

@model Models.Model.SocialMedia
                            <div>
                                @Html.LabelFor(model => model.FullName)
                            </div>
                            <div>
                                @Html.LabelFor(model=>model.Departmen)
                            </div>

And My result is

FullName Departmen

Name,surname and departmen were supposed to come but didn't. Can you help me

CodePudding user response:

I'd have expected something more like:

public ActionResult Index()
{
     
    var model = GetSocialMedia();

    return View(model);
}

private SocialMedia GetSocialMedia()
{
   
    SocialMedia item = new SocialMedia();
    var employee = _employeeRepository.GetById(Session.GetEmployeeNo());           
       //employee.No, employee.ManagerNo
        item.FirstName = employee.FirstName;
        item.LastName = employee.LastName;
        item.Departmen = employee.PositionCode;
    item.FullName = item.FirstName   " "   item.LastName;
    return item;
}

Data in a C# program doesn't appear in place B just because it was created in place A; it has to definitely be passed around so it ends up where it is expected to be

  • Related