C# 如何将数据从模型绑定到ASP控制器?
我正在使用asp.NETMVC3(C#)进行我的项目。我是新来的。我有一个表,其中包含名称、年龄、状态id等数据。现在我希望当我使用asp控件GRIDVIEW时,它应该将我的表的数据绑定到GRIDVIEW,并将2列作为编辑和查看。通过MVC我能做什么?我完全沉浸在这件事中 我还有另外两张桌子 1)C# 如何将数据从模型绑定到ASP控制器?,c#,asp.net-mvc-3,sql-server-2008-r2,C#,Asp.net Mvc 3,Sql Server 2008 R2,我正在使用asp.NETMVC3(C#)进行我的项目。我是新来的。我有一个表,其中包含名称、年龄、状态id等数据。现在我希望当我使用asp控件GRIDVIEW时,它应该将我的表的数据绑定到GRIDVIEW,并将2列作为编辑和查看。通过MVC我能做什么?我完全沉浸在这件事中 我还有另外两张桌子 1) Country\u Master有一列Country\u id、Country\u name 2) City\u Master有一列state\u id、state\u name 我希望当我在下拉列表
Country\u Master
有一列Country\u id、Country\u name
2) City\u Master
有一列state\u id、state\u name
我希望当我在下拉列表中选择国家时,它对应的状态列表应该显示在另一个下拉列表中
当我使用asp控件GRIDVIEW时,它应该将表的数据绑定到
gridview和2列作为编辑和查看。我怎样才能度过难关
MVC
我想你误解了我的一些基本概念。不再有任何服务器端控件,如GridView。在ASP.NET MVC中,经典WebForms中不再使用ViewState或回发模型。因此,在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);
}
}
公共类PersonController:Controller
{
公共行动结果索引()
{
IEnumerable model=…与DAL对话并填充视图模型
返回视图(模型);
}
}
最后,您可以在相应的视图中显示此模型的数据:
@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>
@model IEnumerable
名称
年龄
国家
@foreach(模型中的var人员)
{
@人名
@人.年龄
@个人,国家
}
在ASP.NET MVC视图中,您还可以使用一些内置的帮助程序。例如,有一个允许您简化表格输出的选项:
@model IEnumerable<PersonViewModel>
@{
var grid = new WebGrid();
}
@grid.GetHtml(
grid.Columns(
grid.Column("Name"),
grid.Column("Age"),
grid.Column("Country")
)
)
@model IEnumerable
@{
var grid=new WebGrid();
}
@grid.GetHtml(
网格.列(
网格栏(“名称”),
网格栏(“年龄”),
网格栏(“国家”)
)
)
我建议您阅读关于ASP.NET MVC的教程,以便更好地熟悉基本概念
当我使用asp控件GRIDVIEW时,它应该将表的数据绑定到
gridview和2列作为编辑和查看。我怎样才能度过难关
MVC
我想你误解了我的一些基本概念。不再有任何服务器端控件,如GridView。在ASP.NET MVC中,经典WebForms中不再使用ViewState或回发模型。因此,在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);
}
}
公共类PersonController:Controller
{
公共行动结果索引()
{
IEnumerable model=…与DAL对话并填充视图模型
返回视图(模型);
}
}
最后,您可以在相应的视图中显示此模型的数据:
@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>
@model IEnumerable
名称
年龄
国家
@foreach(模型中的var人员)
{
@人名
@人.年龄
@个人,国家
}
在ASP.NET MVC视图中,您还可以使用一些内置的帮助程序。例如,有一个允许您简化表格输出的选项:
@model IEnumerable<PersonViewModel>
@{
var grid = new WebGrid();
}
@grid.GetHtml(
grid.Columns(
grid.Column("Name"),
grid.Column("Age"),
grid.Column("Country")
)
)
@model IEnumerable
@{
var grid=new WebGrid();
}
@grid.GetHtml(
网格.列(
网格栏(“名称”),
网格栏(“年龄”),
网格栏(“国家”)
)
)
我建议您阅读关于ASP.NET MVC的教程,以便更好地熟悉基本概念。对于gridview,您可以使用或对于gridview,您可以使用或您尝试过一些代码吗?如果是,请发布您是否尝试过一些代码?如果是,请邮寄