简体   繁体   中英

data comes to controller but not to view

 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

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

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