IEnumerable <Mileage>不包含ExpMonthYr的定义

时间:2019-08-27 17:27:07

标签: c# asp.net-mvc asp.net-mvc-5

我添加了@model IEnumerable而不是@model TravelSOCC.Models.LansingMileage,它将不再允许我访问ExpMonthYr

我知道根据文档,这两个功能应该分开,因为它们正在执行不同的任务,只是试图使最终用户浏览的页面更少。

这应该做的是允许用户从日期选择器中选择一个日期,然后更新数据库,并使用@foreach将其重新显示在表中,这意味着我需要在同一视图中但不在同一表中同时使用@Html.EditorFor@Foreach

    @model IEnumerable<TravelSOCC.Models.LansingMileage>

    <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)
            {
                <tr id="">
                    <!--Here is where I will create a table to display my data from the above section-->
                </tr>
            }
        </tbody>
    </table>

2 个答案:

答案 0 :(得分:0)

我了解到您正在尝试在表的前面添加标题输入,datepicker可能用于过滤数据。

在这种情况下,您不能使用Model中的任何属性,因为它是list,并且列表不是您的搜索选项,而是您的搜索结果< / strong>。因此您应该有一个SearchViewModel来将搜索选项传递给服务器,您可以直接将LansingMileage用作SearchViewModel。因此,如果客户输入了什么内容,哪些数据将保存在此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);
}

答案 1 :(得分:0)

您需要一个视图模型来包含LansingMileage记录列表,并为datepicker输入显示单个date属性。您现有的模型(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>

从表单发布中,您将收到一个具有相同模型类型的对象,因此您可以使用如下所示的datepicker输入值:

public ActionResult Test(LansingMileageViewModel model)
{
    DateTime selectedDate = model.ExpMonthYrInput;

    return View(model);
}

HTH