Passing variables from controller to display in view MVC 4 Entity framework

asp.net-mvc asp.net-mvc-4 c# entity-framework

Question

I am trying to display a list of all users registered to my application. I used the register page to both display all users registered and create new users from there.

I'm passing user list to my Register view like this:

[Authorize(Roles="Admin")]
public ActionResult Register()
{
    using (var ctx = new UsersContext())
    {
        return View(ctx.UserProfiles.ToList());
    }
}

I'm unable to loop through ctx.UserProfiles.ToList() from

@foreach (var user in Model)
{
    <tr>
        <td>@user.UserId</td>
        <td>@user.UserName</td>
    </tr>
}

I have @model MvcApplication1.Models.RegisterModel on the top of my cshtml.

I'm new to this, how can I pass a userlist from my controller to view?

I refered to this question here, which did not become help full because I'm unable to add @using MvcApplication1.Models @model IEnumerable<UserProfile> to my page without compilation/parse errors.

1
1
5/23/2017 12:22:21 PM

Accepted Answer

Alright, now we have a little more information, it sounds to me like you want to allow someone to register, whilst listing all UserProfiles. As a view can only accept one model, you need to aggregate RegisterModel and your list of profiles together, and pass that instead:

public class UserProfilesRegisterModel
{
    public RegisterModel RegistrationInfo { get; set; }
    public List<UserProfile> UserProfiles { get; set; }
}

Now your account controller is going to need changing:

[Authorize(Roles="Admin")]
public ActionResult Register()
{
    using (var ctx = new UsersContext())
    {
        var model = new UserProfilesRegisterModel();
        model.UserProfiles = ctx.UserProfiles.ToList();

        return View(model);
    }
}

Now your view will look like this:

@model MvcApplication1.Models.UserProfilesRegisterModel

// rest of view

@foreach (var user in Model.UserProfiles)
{
    <tr>
        <td>@user.UserId</td>
        <td>@user.UserName</td>
    </tr>
}

Note that you'll need to change the parts of your view which access properties on RegisterModel. So for example:

@Html.LabelFor(m => m.UserName)

Would now become:

@Html.LabelFor(m => m.RegistrationInfo.UserName)

Now that the rest is working, you'll need to make a few more changes in AccountControllers Register action. This:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterModel model)

becomes:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(UserProfilesRegisterModel model)

Finally, these:

WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
WebSecurity.Login(model.UserName, model.Password);

become:

WebSecurity.CreateUserAndAccount(model.RegistrationInfo.UserName, model.RegistrationInfo.Password);
WebSecurity.Login(model.RegistrationInfo.UserName, model.RegistrationInfo.Password);
1
1/4/2014 10:23:15 PM

Popular Answer

Use

@model System.Collections.Generic.IEnumerable<MvcApplication1.Models.UserProfile>

that should do the trick if I got your problem right and guessed the namespace.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow