我有一个带有数据表的页面,该页面通过2个不同的按钮执行2个功能。我使用了两种不同的视图模型,一种用于搜索和插入,另一种用于保存数据表列表。
下面的我的查看代码:
@model IEnumerable<ViewModel.TimesheetVM> //TimesheetEntryVM
...
@using (Html.BeginForm("TimesheetEntry", "TimesheetMgmt", FormMethod.Post, new { @enctype = "multipart/form-data" }))
{
// some design codes
<div class="btn-group">
<button type="submit" id="btnInsert" name="btnInsert" class="btn btn-block btn-primary btn-flat"><span class="hide-on-mobile">Insert </span><i class="fa fa-plus"></i></button>
</div> //button to search and insert data in datatable
<table cellspacing="1" cellpadding="2" style="cursor:pointer;width:100%" id="tblTimesheetEntry" class="table table-striped table-hover table-bordered table-hd">
<thead>
<tr class="gridheader">
<td valign="middle" align="center" style="width: 2%;">
</td>
<td>data1</td>
<td>data2</td>
<td>data3</td>
<td>data4</td>
<td>data5</td>
<td>data6</td>
<td>data7</td>
</tr>
</thead>
<tbody>
@if (Model != null)
{
int counter = 1;
foreach (var m in Model)
{
<tr>
<td valign="middle" align="center">
@counter
</td>
<td>@Html.DisplayFor(modelItem => m.data1)</td>
<td>@Html.DisplayFor(modelItem => m.data2)</td>
<td>@Html.DisplayFor(modelItem => m.data3)</td>
<td>@Html.DisplayFor(modelItem => m.data4)</td>
<td>@Html.DropDownList(m.data5, new SelectList(@data5), "--Select Type--", new {@class = "form-control" })</td>
<td>@Html.DropDownList(m.data6, new SelectList(@data6), "--Select Type--", new { @class = "form-control" })</td>
<td>@Html.DisplayFor(modelItem => m.data7)</td>
</tr>
counter=counter+1;
}
}
</tbody>
///after some designs
<div class="btn-group">
<a id="btnConfirm" href="/TimesheetMgmt/TimesheetEntryConfirm" onclick="return confirm('Do you want to Confirm the Timesheet Entry for this month?')" name="btnConfirm" class="btn btn-block btn-success btn-flat"><span class="hide-on-mobile">Confirm</span> <i class="fa fa-save"></i></a>
</div> //button to confirm and save table data.
}
我的控制器代码:
[HttpGet]
public ActionResult TimesheetEntry()
{
return View();
}
[HttpPost]
public ActionResult TimesheetEntry(TimesheetVM model)
{
List<TimesheetVM> _TimesheetEntryVM = new List<TimesheetVM>();
//after some codes
return View(_TimesheetEntryVM);
}
[HttpGet]
public ActionResult TimesheetEntryConfirm(List<TimesheetVM> model)
{
//after some codes
//save confirmed data to database
return RedirectToAction("TimesheetEntry", "TimesheetMgmt");
}
实际上,最初,我使用了2种不同的模型,其中一种是对功能“ TimesheetEntry”使用的“ TimesheetEntryVM”,对函数“ TimesheetEntryConfirm”使用了“ TimesheetVM”,因为我对此并不陌生,并且为这2种模型混淆并更改了相同的视图模型函数-'TimesheetEntry'和'TimesheetEntryConfirm',但是即使我正在使用此模型,模型也会将此函数作为null发送给'TimesheetEntryConfirm'。将数据插入到数据表中可以正常工作,但是发送数据进行保存无法正常工作。如何解决这个问题?请帮忙。