iT邦幫忙

0

抓取View IEnumerable 顯示問題?

Luke 2018-06-11 17:01:252101 瀏覽
  • 分享至 

  • xImage

view model 如下:

public partial class AViewModel
{
    [DisplayName("查詢日期")]
    public string QUERY_DATE { get; set; }
    public List<Header> HeaderInfo { get; set; }
}

public partial class Header
{        
    public string Order { get; set; }
    public int Qty { get; set; }
    public Datetime Date { get; set; }
    public string PartNo { get; set; }
}

controller如下:

 public ActionResult QueryInfo(AViewModel vm)
 {
    string qDate = vm.QUERY_DATE;
    vm.HeaderList = db.QueryHeaderList(qDate);
    return View(vm);
}
public ActionResult QueryInfo()
{ 
    var vm = new AViewModel();
    string qDate = DateTime.Today.ToString("yyyyMMdd");
    vm.QUERY_DATE = qDate;
    vm.HeaderList = db.QueryHeaderList(qDate);
    return View();
}

view 如下:

@using (Html.BeginForm("QueryInfo", "QueryInfo", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true, "", new { })
    <table style="width:100%; border-spacing:0;">
        <tr>
            <th class="header">@Html.LabelFor(model => model.QUERY_DATE)</th>
            <td>
                @Html.TextBoxFor(model => model.QUERY_DATE)
                @Html.ValidationMessageFor(vm => vm.QUERY_DATE, "", new { })
            </td>
            <th class="header">&nbsp;</th>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td colspan="4" align="right"><input type="submit"></td>
        </tr>
        </table>
        <table style="width:95%; border-spacing:1;">
        @foreach (var item in Model.HeaderList)
        {
        <tr>
            <td>@Html.LabelFor(model => item.Order)</td>
            <td>@Html.LabelFor(model => item.Qty)</td>
            <td>@Html.LabelFor(model => item.PartNo)</td>
        </tr>
        }
        <tr><td>=================================</td></tr>
        @for (int i = 0; i < Model.HeaderList.Count; i++)
        {
        <tr>
            <td>@Html.LabelFor(m => m.HeaderList[i].Order)</td>
            <td>@Html.LabelFor(m => m.HeaderList[i].Qty)</td>
            <td>@Html.LabelFor(m => m.HeaderList[i].PartNo)</td>
        </tr>
        }
        </table>
        
}

為什無法顯示數據? 都是顯示 Order 、 Qty、 PartNo
請問是哪個地方設定錯誤?

Homura iT邦高手 1 級 ‧ 2018-06-11 17:22:18 檢舉
你View最上面沒宣告你的Model從哪來的?
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

2
暐翰
iT邦大師 1 級 ‧ 2018-06-11 22:52:47
最佳解答

問題點在:

1.把Html.LabelFor當成屬性值,它實際是輸出屬性名稱(or DisPlayName)。
所以才都顯示Order、QTY、PartNO
2.表頭不需要foreach
3.表身用foreach + 使用item.屬性名稱就可以抓取值


我寫的線上測試DEMO連結:

C# Online Compiler | .NET Fiddle

@model HelloWorldMvcApp.AViewModel
@{
	Layout = null;

}

@using (Html.BeginForm("QueryInfo", "QueryInfo", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true, "", new { })
    <table style="width:100%; border-spacing:0;">
        <tr>
            <th class="header">@Html.LabelFor(model => model.QUERY_DATE)</th>
            <td>
                @Html.TextBoxFor(model => model.QUERY_DATE)
                @Html.ValidationMessageFor(vm => vm.QUERY_DATE, "", new { })
            </td>
            <th class="header"> </th>
            <td> </td>
        </tr>
        <tr>
            <td colspan="4" align="right"><input type="submit"></td>
        </tr>
    </table>

    <table style="width:95%; border-spacing:1;">
        <tr>
            <th>Order</th>
            <th>Qty</th>
            <th>PartNo</th>
        </tr>
        <tr><td><hr></td><td><hr></td><td><hr></td></tr>
        @foreach(var item in Model.HeaderList){
        <tr>
            <td>@item.Order</td>
            <td>@item.Qty</td>
            <td>@item.PartNo</td>
        </tr>			
        }
    </table>		
        
}
Luke iT邦研究生 5 級 ‧ 2018-06-12 08:21:38 檢舉

/images/emoticon/emoticon82.gif

了解,謝謝
我觀念錯誤
謝謝

我要發表回答

立即登入回答