8

注:これは、MVCではなく.NET4.5でのASP.NETWebフォームモデルバインディングです。

ASP.NET Webフォーム(4.5)の新しい厳密に型指定されたモデルバインディング機能を使用して、編集可能なアイテムのリストを作成しています。これは、初期リストの表示、アイテムの編集、およびアイテムの削除に問題なく機能します。しかし、新しいアイテムの挿入に問題があります。

具体的には、EditItemTemplateとInsertItemTemplate内にDropDownListがあります(実際には、DropDownListから派生したカスタムコントロールですが、この質問の目的ではDropDownListです)。コントロールは、マークアップ内で次のように定義されています。

<agp:ClientStatusDropDownList ID="ClientStatusID" runat="server"
SelectedValue="<%#: BindItem.ClientStatusID %>" />

EditItemTemplate内ではこれで問題ありませんが、InsertItemTemplate内では、ページの実行時にエラーが発生します。Eval ()、XPath()、Bind()などのデータバインディングメソッドは、データバインドされたコントロールのコンテキストでのみ使用できます。

そのためSelectedValue="<%#: BindItem.ClientStatusID %>"、InsertItemTemplateからセクションを削除して、再試行しました。今回はエラーメッセージは表示されませんが、ListView.InsertMethodが呼び出されると、モデルのClientStatusIDプロパティがDropDownListの値に設定されません(残りのプロパティは正しく設定されます)。

ListView.InsertMethod:

public void ListView_InsertMethod(int ID) {

  Model model = this.DbContext.Models.Create();
  if (this.TryUpdateModel(model)) {
    this.DbContext.SaveChanges();
    this.ListView.DataBind();
  }

}

モデルクラス:

public class Model{

  public Int32 ID { get; set; }
  public String Description { get; set; }
  public Boolean IsScheduleFollowUp { get; set; }
  public Nullable<Int32> ClientStatusID { get; set; }

}

EditItemTemplate:

<EditItemTemplate>
  <tr>
    <td>
      <asp:TextBox ID="Description" runat="server" Text="<%#: BindItem.Description %>" />
    </td>
    <td>
      <asp:CheckBox ID="IsScheduleFollowUp" runat="server" Checked="<%# BindItem.IsScheduleFollowUp %>" />
    </td>
    <td>
      <agp:ClientStatusDropDownList ID="ClientStatusID" runat="server" SelectedValue="<%#: BindItem.ClientStatusID %>" />
    </td>
    <td>
      <asp:Button ID="Update" runat="server" ClientIDMode="Static" CommandName="Update" Text="Update" />
      <asp:Button ID="Cancel" runat="server" ClientIDMode="Static" CommandName="Cancel" Text="Cancel" />
    </td>
  </tr>
</EditItemTemplate>

InsertItemTemplate:

<InsertItemTemplate>
  <tr>
    <td>
      <asp:TextBox ID="Description" runat="server" Text="<%#: BindItem.Description %>" />
    </td>
    <td>
      <asp:CheckBox ID="IsScheduleFollowUp" runat="server" Checked="<%# BindItem.IsScheduleFollowUp %>" />
    </td>
    <td>
      <agp:ClientStatusDropDownList ID="ClientStatusID" runat="server" />
    </td>
    <td>
      <asp:Button ID="Insert" runat="server" ClientIDMode="Static" CommandName="Insert" Text="Add" />
    </td>
  </tr>
</InsertItemTemplate>

私は当初、値が渡されるモデルのプロパティを決定するために使用されるのはコントロールのIDだと思っていました(つまり、TextBoxが「Description」と呼ばれる場合、値は「Description」に渡されます)モデルのプロパティ)。明らかにこれは当てはまらず、代わりに「<%#BindItem.Description%>」によって制御されますが、この質問の残りの部分からわかるように、「InsertItemTemplate」でこの構文を使用することはできません。このシナリオでDropDownListがサポートされていないことは信じられませんが、GoogleまたはBingを使用した4.5モデルバインディングで使用されているDropDownListの例は見つかりません(実際、ASPでの新しいモデルバインディングの例はほとんどありません。いくつかのTextBoxコントロール以外のものを使用するNET4.5)。

誰かがこの問題にさらに光を当てることができますか(そしてできれば何をする必要があるか教えてください)?

私が見たSOに関する他の質問...

これらはすべて、4.5の新しいメソッドではなく、古いスタイルのバインディングメソッドを使用します

ありがとう。

4

1 に答える 1

6

私は似たようなことに取り組んでいて、サンプルを機能させることができたので、私が持っているものを投稿して、これが役立つかどうかを確認することにしました.

ここに私のページコードがあります:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="listview-databind.aspx.cs"
    Inherits="test_listview_databind" Debug="true" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ListView ID="lv" runat="server" ItemType="DataModel" DataKeyNames="Id" SelectMethod="lv_GetData"
            InsertItemPosition="FirstItem" InsertMethod="lv_InsertItem" UpdateMethod="lv_UpdateItem">
            <LayoutTemplate>
                <table>
                    <tr id="itemPlaceholder" runat="server"></tr>
                </table>
            </LayoutTemplate>
            <ItemTemplate>
                <tr>
                    <td>
                        <asp:Literal ID="Description" runat="server" Text="<%# Item.Description %>" />
                    </td>
                    <td>
                        <asp:CheckBox ID="IsScheduleFollowUp" runat="server" Checked="<%# Item.IsScheduleFollowUp %>" />
                    </td>
                    <td>
                        <asp:Literal ID="ClientStatusId" runat="server" />
                    </td>
                    <td>
                        <asp:Button ID="Edit" runat="server" ClientIDMode="Static" CommandName="Edit"
                            Text="Edit" />
                    </td>
                </tr>
            </ItemTemplate>
            <InsertItemTemplate>
                <tr>
                    <td>
                        <asp:TextBox ID="Description" runat="server" Text="<%# BindItem.Description %>" />
                    </td>
                    <td>
                        <asp:CheckBox ID="IsScheduleFollowUp" runat="server" Checked="<%# BindItem.IsScheduleFollowUp %>" />
                    </td>
                    <td>
                        <asp:DropDownList ID="ClientStatusId" runat="server" SelectedValue="<%# BindItem.ClientStatusId %>">
                            <asp:ListItem>1</asp:ListItem>
                            <asp:ListItem>2</asp:ListItem>
                        </asp:DropDownList>
                    </td>
                    <td>
                        <asp:Button ID="Insert" runat="server" ClientIDMode="Static" CommandName="Insert"
                            Text="Add" />
                        <asp:Button ID="Cancel" runat="server" ClientIDMode="Static" CommandName="Cancel"
                            Text="Cancel" />
                    </td>
                </tr>
            </InsertItemTemplate>
            <EditItemTemplate>
                <tr>
                    <td>
                        <asp:TextBox ID="Description" runat="server" Text="<%# BindItem.Description %>" />
                    </td>
                    <td>
                        <asp:CheckBox ID="IsScheduleFollowUp" runat="server" Checked="<%# BindItem.IsScheduleFollowUp %>" />
                    </td>
                    <td>
                        <asp:DropDownList ID="ClientStatusId" runat="server" SelectedValue="<%# BindItem.ClientStatusId %>">
                            <asp:ListItem>1</asp:ListItem>
                            <asp:ListItem>2</asp:ListItem>
                        </asp:DropDownList>
                    </td>
                    <td>
                        <asp:Button ID="Update" runat="server" ClientIDMode="Static" CommandName="Update"
                            Text="Update" />
                        <asp:Button ID="Cancel" runat="server" ClientIDMode="Static" CommandName="Cancel"
                            Text="Cancel" />
                    </td>
                </tr>
            </EditItemTemplate>
        </asp:ListView>
    </form>
</body>
</html>

そして私のコードビハインド:

using System.Collections.Generic;
using System.Linq;

public partial class test_listview_databind : System.Web.UI.Page
{
    public IQueryable<DataModel> lv_GetData()
    {
        var l = new List<DataModel>();
        l.Add(new DataModel() { Id = 1, Description = "Test 1", IsScheduleFollowUp = true, ClientStatusId = 1 });
        l.Add(new DataModel() { Id = 2, Description = "Test 2", IsScheduleFollowUp = false, ClientStatusId = 2 });
        return l.AsQueryable();
    }

    public void lv_InsertItem()
    {
        var item = new DataModel();
        TryUpdateModel(item);
        if (ModelState.IsValid)
        {
            Response.Write(item.ClientStatusId);
        }
    }
}

ListView サンプル全体を投稿していないので、どのように設定されているか推測しています。これが役立つかどうか、およびこれが機能するかどうか/どのように機能するかをお知らせください。コードが機能しているように見え、問題の原因について知りたいです。

于 2013-02-24T18:24:15.867 に答える