亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 135 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號