2 回答

TA貢獻1775條經驗 獲得超8個贊
您需要一個視圖模型來包含記錄列表LansingMileage并為日期選擇器輸入公開單個日期屬性。您現有的模型(IEnumerable)本身只能綁定到項目集合(除非您嘗試一些技巧,例如 model[0]),因此建議您創建一個涵蓋視圖要求的視圖模型。
例如:
namespace TravelSOCC.ViewModels
{
public class LansingMileageViewModel
{
public IEnumerable<TravelSOCC.Models.LansingMileage> Records { get; set; }
public DateTime ExpMonthYrInput { get; set; }
}
}
然后您的視圖將使用如下所示的視圖模型:
@model TravelSOCC.ViewModels.LansingMileageViewModel
<h3>STATE OFFICERS COMPENSATION COMMISSION (SOCC)</h3>
@using (Html.BeginForm())
{
<table class="table">
<tr>
<th>Senator Information</th>
<th>Monthly Expense Summary</th>
<th>Expense Month/Year</th>
</tr>
<tr>
<td>State Senator</td>
<td>Mileage Total:<br />Expense Total:<br /><strong>Total:</strong></td>
<!--This will become a datepicker-->
<td>@Html.EditorFor(model => model.ExpMonthYr, new { htmlAttributes = new { @class = "datepicker", Name = "expenseDate" } })</td>
</tr>
</table>
}
<table id="LansingData" class="display">
<thead>
<tr>
<th>Expense Month/Yr</th>
<th>Travel Date</th>
<th>Travel Type</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Records)
{
<tr id="">
<!--Here is where I will create a table to display my data from the above section-->
</tr>
}
</tbody>
</table>
從您的表單發布中,您將收到相同模型類型的對象,因此您可以使用日期選擇器輸入值,如下所示:
public ActionResult Test(LansingMileageViewModel model)
{
DateTime selectedDate = model.ExpMonthYrInput;
return View(model);
}

TA貢獻1873條經驗 獲得超9個贊
我的理解是,您正在嘗試在表格前面添加標題輸入,datepicker可能用于過濾數據。
在這種情況下,您不能使用 中的任何屬性Model,因為它是 alist并且列表不是您的搜索選項而是您的搜索結果。所以你應該有一個SearchViewModel將搜索選項傳遞到服務器,你可以直接使用LansingMileageas SearchViewModel。因此,如果客戶端輸入某些內容,哪些數據將保存在 this 中SearchViewModel。
這是示例,主要是 ASP.NET Core 代碼,但它與 MVC5 的模式非常相似。
在cshtml的開頭:
@{
var searchModel = ViewBag.SearchModel as LansingMileage;
}
在您的搜索表單中:
@Html.EditorFor(model => searchModel.ExpMonthYr, new { htmlAttributes = new { @class = "datepicker", Name = "expenseDate" } })
在服務器端,
public async Task<IActionResult> Search([FromForm]LansingMileage searchModel, int page = 0, int pageSize = 15)
{
searchModel = searchModel ?? new LansingMileage();
var data= GetData(searchModel, page, pageSize);
ViewBag.SearchModel = searchModel;
return View(data);
}
- 2 回答
- 0 關注
- 135 瀏覽
添加回答
舉報