I am currently have a bunch of users sitting in a Membership Database and I want to be able to create a page in an MVC App that will be designed to pull back all the user information and display it on the page, I have created the solution below however I want be able to show the data in a Grid View instead of a list:
我目前有很多用户坐在成员数据库,我希望能够在MVC应用程序中创建一个页面将被设计来拉回所有的用户信息并将其显示在页面上,然而我希望我创建了以下解决方案能够显示数据网格视图而不是列表中:
Controller:
控制器:
public ActionResult Index()
{
var systemUsers = Membership.GetAllUsers();
return View(systemUsers);
}
View:
观点:
<ul>
<%foreach (MembershipUser user in Model){ %>
<li><%=user.UserName %></li>
<% }%>
</ul>
Can someone please tell me how I can get the users data and display it in a Grid View on the View? I was thinking that I could have a model that stores the data in which is then passed to the view and this model could be updated by the controller? But im unsure which datatype could be used to store the users data and then passed to the Grid View?
有人能告诉我如何获得用户数据并在视图的网格视图中显示它吗?我想我可以有一个模型来存储数据然后传递给视图这个模型可以被控制器更新吗?但是我不确定哪一个数据类型可以用来存储用户数据,然后传递到网格视图?
Thanks :-)
谢谢:-)
1 个解决方案
#1
1
Using MembershipUser
directly in your views isn't a very pure MVC approach, IMO.
在您的视图中直接使用MembershipUser并不是一个非常纯粹的MVC方法,IMO。
IMO you could create a specific ViewModel containing just the fields you need to pass between the controller and the view, like so:
在我看来,您可以创建一个特定的ViewModel,其中只包含您需要在控制器和视图之间传递的字段,如下所示:
public class UserViewModel
{
public Guid UserId {get; set;}
public string UserName {get; set;}
// etc
}
After fetching all your users in your controller, you can then project the MembershipUsers across to your UserViewModel objects, e.g. using LINQ or AutoMapper, and then pass this enumeration into the View.
在获取控制器中的所有用户之后,您就可以将MembershipUsers映射到您的UserViewModel对象,例如使用LINQ或AutoMapper,然后将此枚举传递到视图中。
public ActionResult Index()
{
var systemUsers = Membership.GetAllUsers();
var vmUsers = new List<UserViewModel>();
foreach (MembershipUser u in Membership.GetAllUsers())
{
vmUsers.Add(new UserViewModel()
{
UserId = (Guid)u.ProviderUserKey,
UserName = u.UserName,
// etc
});
}
return View(vmUsers);
}
I'm not sure what you mean by 'GridView' (this is MVC, not WebForms), but the MVC WebGrid should do fine:
我不确定你所说的“GridView”(这是MVC,不是WebForms)是什么意思,但是MVC WebGrid应该做得很好:
@model IEnumerable<MyNamespace.UserViewModel>
@{
var grid = new WebGrid(Model);
}
<div id="grid">
@grid.GetHtml()
</div>
Obviously you'll want to tweak the look and feel a bit.
显然,你会想调整一下外观和感觉。
#1
1
Using MembershipUser
directly in your views isn't a very pure MVC approach, IMO.
在您的视图中直接使用MembershipUser并不是一个非常纯粹的MVC方法,IMO。
IMO you could create a specific ViewModel containing just the fields you need to pass between the controller and the view, like so:
在我看来,您可以创建一个特定的ViewModel,其中只包含您需要在控制器和视图之间传递的字段,如下所示:
public class UserViewModel
{
public Guid UserId {get; set;}
public string UserName {get; set;}
// etc
}
After fetching all your users in your controller, you can then project the MembershipUsers across to your UserViewModel objects, e.g. using LINQ or AutoMapper, and then pass this enumeration into the View.
在获取控制器中的所有用户之后,您就可以将MembershipUsers映射到您的UserViewModel对象,例如使用LINQ或AutoMapper,然后将此枚举传递到视图中。
public ActionResult Index()
{
var systemUsers = Membership.GetAllUsers();
var vmUsers = new List<UserViewModel>();
foreach (MembershipUser u in Membership.GetAllUsers())
{
vmUsers.Add(new UserViewModel()
{
UserId = (Guid)u.ProviderUserKey,
UserName = u.UserName,
// etc
});
}
return View(vmUsers);
}
I'm not sure what you mean by 'GridView' (this is MVC, not WebForms), but the MVC WebGrid should do fine:
我不确定你所说的“GridView”(这是MVC,不是WebForms)是什么意思,但是MVC WebGrid应该做得很好:
@model IEnumerable<MyNamespace.UserViewModel>
@{
var grid = new WebGrid(Model);
}
<div id="grid">
@grid.GetHtml()
</div>
Obviously you'll want to tweak the look and feel a bit.
显然,你会想调整一下外观和感觉。