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.
Use
@model System.Collections.Generic.IEnumerable<MvcApplication1.Models.UserProfile>
that should do the trick if I got your problem right and guessed the namespace.
Alright, now we have a little more information, it sounds to me like you want to allow someone to register, whilst listing all UserProfile
s. 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 AccountController
s 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);
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.