为了账号安全,请及时绑定邮箱和手机立即绑定

IEnumerable<Mileage> 不包含 ExpMonthYr 的定义

IEnumerable<Mileage> 不包含 ExpMonthYr 的定义

C#
繁华开满天机 2023-09-24 15:58:33
我添加了 @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 回答

?
www说

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);

}



查看完整回答
反对 回复 2023-09-24
?
眼眸繁星

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);

}


查看完整回答
反对 回复 2023-09-24
  • 2 回答
  • 0 关注
  • 61 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信