Masriyah
Masriyah

Reputation: 2515

connecting controller with model to display results in view page

So i have this aps.net mvc project in which i created a service layer, model views, controller, and a view page. But i am having trouble displaying my results to the view page. I am starting this would by passing in a specific linq statement in the service layer so i should be able to return it to show up on the view. Here is what i have:

Service:

public IEnumerable<RoleUser> GetUsers(int sectionID)
    {
        var _role = DataConnection.GetRole<RoleUser>(9, r => new RoleUser
        {
            Name = RoleColumnMap.Name(r),
            Email = RoleColumnMap.Email(r)
        }, resultsPerPage: 20, pageNumber: 1);
        return _role;
    }

Models:

public partial class Role
{
    public RoleView()
    {
        this.Users = new HashSet<RoleUser>();
    }
    public ICollection<RoleUser> Users { get; set; }
}

public class RoleUser
{
    public string Name { get; set; }
    public string Email { get; set; }
}

Controller:

public ActionResult RoleUser(RoleView rvw)
    {
        var rosterUser = new RosterService().GetUsers();
        ViewBag.RosterUsers = rosterUser;
        return View();
    }

View:

<div>
<span>@Model.Name</span>
</div>

I am not sure what i am missing or doing wrong but any tips will be great. I basically want to return the results from the linq statement i am testing to see that the connection is correct and functionality is there before enhancing. Thanks...

Upvotes: 1

Views: 192

Answers (1)

Mike Perrenoud
Mike Perrenoud

Reputation: 67918

Well, if I were to go off the code you've provided I would say that I'm unsure how this compiles:

public partial class Role
{
    public RoleView()
    {
        this.Users = new HashSet<RoleUser>();
    }
    public ICollection<RoleUser> Users { get; set; }
}

it feels like that should be:

public partial class RoleView

and then I would say that at the top of your view you're missing this:

@model NamespaceToClass.RoleView

and then I would say you're not going to be able to issue this:

@Model.Name

because RoleUser isn't your model. You're going to need to loop through the users:

@foreach (RoleUser ru in Model.Users)

and then inside that loop you can build some HTML with this:

ru.Name

but I would also question your controller. Right now it's receiving a model to return that model. There is some code missing here but generally speaking, inside the method:

public ActionResult RoleUser(RoleView rvw)

you would actually go get the data, construct the model, and then return that:

var users = serviceLayer.GetUsers(...);

// now construct the RoleView model
var model = ...

return View(model);

Based off of our conversation you currently have something like this in your controller:

public ActionResult View(int id) 
{ 
    // get the menu from the cache, by Id 
    ViewBag.SideBarMenu = SideMenuManager.GetRootMenu(id); 
    return View(); 
} 

public ActionResult RoleUser(RoleView rvw) 
{ 
    var rosterUser = new RosterService().GetUsers(); 
    ViewBag.RosterUsers = rosterUser; 
    return View(); 
}

but that really needs to look like this:

public ActionResult View(int id) 
{ 
    // get the menu from the cache, by Id 
    ViewBag.SideBarMenu = SideMenuManager.GetRootMenu(id); 

    var rosterUser = new RosterService().GetUsers(); 
    ViewBag.RosterUsers = rosterUser; 

    return View(); 
} 

because you're launching this page from the sidebar which is hitting this action because you're passing the id in the URL. You don't even need the other action.

Upvotes: 1

Related Questions