我正在为我的项目使用asp.net mvc3(C#)。在这方面我是个新手。我有一个表,其中有数据,如姓名,年龄,state_id。现在我希望当我使用asp控件GRIDVIEW时,它应该将表格的数据绑定到网格视图,并将2列绑定为编辑和视图。如何通过MVC进行操作?我完全沉浸在其中了。
我还有另外两张桌子
1)Country_Master有列country_id,country_name
2)City_Master有state_id,state_name列。
我希望当我在下拉列表中选择国家时,其对应的州列表应该显示在另一个下拉列表中。
发布于 2013-01-29 22:40:59
当我使用asp控件GRIDVIEW时,它应该将表格的数据绑定到网格视图,并将2列绑定为编辑和视图。如何通过MVC进行操作?
我认为你误解了ASP.NET MVC中的一些基本概念。不再有任何服务器端控件,如GridView。在ASP.NET MVC中,不再有经典WebForms中使用的ViewState和PostBack模型。由于这个原因,您可能在WebForms中使用的服务器端控件都不能在ASP.NET MVC中工作。这是一种完全不同的web开发方法。
在ASP.NET MVC中,您可以从定义一个保存数据的模型开始:
public class PersonViewModel
{
public string Name { get; set; }
public int Age { get; set; }
public string Country { get; set; }
}然后是一个控制器,它将与您的DAL对话并填充模型:
public class PersonController: Controller
{
public ActionResult Index()
{
IEnumerable<PersonViewModel> model = ... talk to your DAL and populate the view model
return View(model);
}
}最后,您有一个相应的视图,您可以在其中显示此模型的数据:
@model IEnumerable<PersonViewModel>
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Country</th>
</tr>
</thead>
<tfoot>
@foreach (var person in Model)
{
<tr>
<td>@person.Name</td>
<td>@person.Age</td>
<td>@person.Country</td>
</tr>
}
</tfoot>
</table>在ASP.NET MVC视图中,您还可以使用一些内置的助手。例如,有一个WebGrid helper允许您简化表格输出:
@model IEnumerable<PersonViewModel>
@{
var grid = new WebGrid();
}
@grid.GetHtml(
grid.Columns(
grid.Column("Name"),
grid.Column("Age"),
grid.Column("Country")
)
)我建议您阅读关于ASP.NET MVC的getting started tutorials,以便更好地熟悉基本概念。
发布于 2013-01-29 22:48:23
对于网格视图,您可以使用MvcContrib Grid或JQuery Grid for MVC
https://stackoverflow.com/questions/14585339
复制相似问题