IEnumerable<Mileage> 不包含 ExpMonthYr 的定义

我添加了 @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>


繁华开满天机
浏览 66回答 2
2回答

www说

您需要一个视图模型来包含记录列表LansingMileage并为日期选择器输入公开单个日期属性。您现有的模型(IEnumerable)本身只能绑定到项目集合(除非您尝试一些技巧,例如 model[0]),因此建议您创建一个涵盖视图要求的视图模型。例如:namespace TravelSOCC.ViewModels{&nbsp; &nbsp; public class LansingMileageViewModel&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public IEnumerable<TravelSOCC.Models.LansingMileage> Records { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public DateTime ExpMonthYrInput { get; set; }&nbsp; &nbsp; }}然后您的视图将使用如下所示的视图模型:@model TravelSOCC.ViewModels.LansingMileageViewModel<h3>STATE OFFICERS COMPENSATION COMMISSION (SOCC)</h3>@using (Html.BeginForm()){&nbsp; &nbsp; <table class="table">&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Senator Information</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Monthly Expense Summary</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Expense Month/Year</th>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>State Senator</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Mileage Total:<br />Expense Total:<br /><strong>Total:</strong></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!--This will become a datepicker-->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>@Html.EditorFor(model => model.ExpMonthYr, new { htmlAttributes = new { @class = "datepicker", Name = "expenseDate" } })</td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </table>}<table id="LansingData" class="display">&nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Expense Month/Yr</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Travel Date</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Travel Type</th>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </thead>&nbsp; &nbsp; <tbody>&nbsp; &nbsp; &nbsp; &nbsp; @foreach (var item in Model.Records)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr id="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!--Here is where I will create a table to display my data from the above section-->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </tbody></table>从您的表单发布中,您将收到相同模型类型的对象,因此您可以使用日期选择器输入值,如下所示:public ActionResult Test(LansingMileageViewModel model){&nbsp; &nbsp; DateTime selectedDate = model.ExpMonthYrInput;&nbsp; &nbsp; return View(model);}

眼眸繁星

我的理解是,您正在尝试在表格前面添加标题输入,datepicker可能用于过滤数据。在这种情况下,您不能使用 中的任何属性Model,因为它是 alist并且列表不是您的搜索选项而是您的搜索结果。所以你应该有一个SearchViewModel将搜索选项传递到服务器,你可以直接使用LansingMileageas SearchViewModel。因此,如果客户端输入某些内容,哪些数据将保存在 this 中SearchViewModel。这是示例,主要是 ASP.NET Core 代码,但它与 MVC5 的模式非常相似。在cshtml的开头:@{&nbsp; &nbsp;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){&nbsp; &nbsp; searchModel = searchModel ?? new LansingMileage();&nbsp; &nbsp; var data= GetData(searchModel, page, pageSize);&nbsp; &nbsp; ViewBag.SearchModel = searchModel;&nbsp; &nbsp; return View(data);}
打开App,查看更多内容
随时随地看视频慕课网APP