我有一些数据显示在View
HTML
表中。现在,我想做的是,在单击按钮(例如SUBMIT
按钮(时,我想将数据发送到控制器的 POST 方法,以便我可以将数据保存到数据库中。
我尝试使用Model Binding
技术获取数据,但是,POST
方法中的ViewModel
对象是null
。
和视图模型
// Model Model.cs
public class MyModel
{
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
...
}
// ViewModel MyViewModel.cs
public class MyViewModel
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
...
public List<MyModel> MyModelList { get; set; }
}
// ViewModel VMList.cs
public class VMList
{
public List<MyViewModel> MyViewModelList { get; set; }
}
所以,我有一个名为MyModel.cs
的Model
,它是数据库中的表。然后,我有一个名为MyViewModel.cs' that has the same columns as the
模型, in addition to some columns, plus a
列表ViewModel
of
MyModel type. Then, there is another
ViewModel called
VMList.cs that contains a list of tuples of
MyViewModel type. This
ViewModel is passed to the
View'。
视图按以下方式构造:
视图
@model ...Models.ViewModels.VMList
...
<form asp-action="MyAction" asp-controller="MyController" id="myForm">
<div>
<button type="submit" value="submit" id="submitButton">Submit</button>
<table id="myTable">
@foreach(var item in Model.MyViewModelList)
{
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Header 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>
@item.FirstName
</td>
<td>
@item.LastName
</td>
<td>
@item.Header3
</td>
</tr>
@if(item.subList != null && item.subList.Count() != 0)
{
<thead>
<tr>
<th>SubList Header 1</th>
<th>SubList Header 2</th>
<th>SubList Header 3</th>
</tr>
</thead>
<tbody>
@foreach(var subItem in item.subList)
{
<tr>
<td>
@subItem.SubListHeader1
</td>
<td>
@subItem.SubListHeader2
</td>
<td>
@subItem.SubListHeader3
</td>
</tr>
}
</tbody>
}
}
</table>
</div>
</form>
开机自检方法
[HttpPost]
public IActionResult MyAction(VMList vmListObject)
{
return View();
}
如何将View
表中显示的数据恢复到Controller
?
根据您给出的模型,尝试使用asp-for
标记帮助程序和name
属性进行模型绑定。
@model VMList
<form asp-action="MyAction" asp-controller="HomeController" id="myForm">
<div>
<button type="submit" value="submit" id="submitButton">Submit</button>
<table id="myTable">
@{ int i = 0;}
@foreach (var item in Model.MyViewModelList)
{
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Header 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input asp-for="@item.FirstName" name="vmListObject.MyViewModelList[@i].FirstName" />
</td>
<td>
<input asp-for="@item.LastName" name="vmListObject.MyViewModelList[@i].LastName"/>
</td>
<td>
<input asp-for="@item.Header3" name="vmListObject.MyViewModelList[@i].Header3" />
</td>
</tr>
@if (item.MyModelList != null && item.MyModelList.Count() != 0)
{
<thead>
<tr>
<th>SubList Header 1</th>
<th>SubList Header 2</th>
<th>SubList Header 3</th>
</tr>
</thead>
<tbody>
@{ int j = 0;}
@foreach (var subItem in item.MyModelList)
{
<tr>
<td>
<input asp-for="@subItem.FirstName" name="vmListObject.MyViewModelList[@i].MyModelList[@j].FirstName"/>
</td>
<td>
<input asp-for="@subItem.LastName" name="vmListObject.MyViewModelList[@i].MyModelList[@j].LastName" />
</td>
<td>
test
</td>
</tr>
j++;
}
</tbody>
}
</tbody>
i++;
}
</table>
</div>
</form>