2013-02-18 36 views
8

नोट: यह एएसपी.NET वेब फॉर्म मॉडल बाध्यकारी .NET 4.5 और एमवीसी में नहीं है।एएसपी.नेट वेब फॉर्म (4.5) मजबूत टाइप किए गए मॉडल बाध्यकारी - InsDIemem में DropDownList ListView

मैं संपादित किए जा सकने वाले आइटमों की एक सूची तैयार करने के लिए एएसपी.नेट वेब फॉर्म (4.5) की नई मजबूत टाइपेड मॉडल बाध्यकारी विशेषताओं का उपयोग कर रहा हूं। यह प्रारंभिक सूची देखने, किसी आइटम को संपादित करने और किसी आइटम को हटाने के लिए ठीक काम कर रहा है। हालांकि मुझे एक नई वस्तु के सम्मिलन में समस्या है।

विशेष रूप से, मेरे संपादन IememTemplate और InsertItemTemplate में मेरे पास ड्रॉपडाउनलिस्ट है (ठीक है, वास्तव में यह ड्रॉपडाउनलिस्ट से प्राप्त कस्टम नियंत्रण है लेकिन इस प्रश्न के प्रयोजनों के लिए यह ड्रॉपडाउनलिस्ट है)। नियंत्रण मार्कअप के रूप में इस प्रकार के भीतर परिभाषित किया गया है ...

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

EditItemTemplate के भीतर, यह तथापि InsertItemTemplate यह पेज चल रहे पर एक त्रुटि उत्पन्न करता है के भीतर ठीक है: इस तरह के eval(), XPath (के रूप में Databinding तरीकों), और बाइंड() केवल डाटाबेस नियंत्रण के संदर्भ में उपयोग किया जा सकता है।

इस तरह, मैंने InsertItemTemplate से SelectedValue="<%#: BindItem.ClientStatusID %>" अनुभाग को हटा दिया और फिर से प्रयास किया। इस बार कोई त्रुटि संदेश नहीं है, हालांकि ListView.InsertMethod कहा जाता है, मॉडल पर ClientStatusID प्रॉपर्टी ड्रॉपडाउनलिस्ट के मान पर सेट नहीं है (जबकि बाकी गुण सही ढंग से सेट हैं)।

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> 

मैंने मूल रूप से सोचा था कि यह उस नियंत्रण की आईडी थी जिसका उपयोग मॉडल पर संपत्ति निर्धारित करने के लिए किया गया था कि मूल्य पारित किया जाएगा (यानी। जहां टेक्स्टबॉक्स को "विवरण" कहा जाता था, मान मॉडल की "विवरण" संपत्ति को पास किया जाएगा)। स्पष्ट रूप से यह मामला नहीं है और इसके बजाय इसे "<% # बाइंड इटिम। डिस्क्रिप्शन%>" द्वारा नियंत्रित किया जाता है, हालांकि, जैसा कि आप इस शेष प्रश्न से देख सकते हैं, मैं "InsertItemTemplate" में इस वाक्यविन्यास का उपयोग करने में असमर्थ हूं। मुझे विश्वास नहीं है कि DropDownList इस परिदृश्य में समर्थित नहीं है, लेकिन मुझे Google या Bing का उपयोग करके 4.5 मॉडल बाइंडिंग के साथ उपयोग किए जाने वाले ड्रॉपडाउनलिस्ट का कोई उदाहरण नहीं मिल रहा है (वास्तव में, एएसपी में नए मॉडल बाध्यकारी के बहुत कम उदाहरण हैं। नेटबॉक्स 4.5 टेक्स्टबॉक्स नियंत्रण के अलावा कुछ भी उपयोग कर)।

क्या कोई इस मुद्दे पर कोई और प्रकाश डाल सकता है (और अधिमानतः मुझे बताएं कि क्या करने की आवश्यकता है)?

एसओ पर अन्य प्रश्न जो मैंने देखा है ...

इन सभी में 4.5

धन्यवाद पुरानी शैली बाध्यकारी तरीकों और नहीं नए तरीकों का उपयोग करें।

+0

बस संदर्भ के लिए; मैंने वैकल्पिक मॉडल को बाध्य करने का भी प्रयास किया है जहां क्लाइंटस्टैटस आईडी शून्य नहीं है (यह कोड-पीछे है, मार्कअप नहीं है)। कोई फर्क नहीं पड़ता, संपत्ति अभी भी सेट नहीं है। यह (मेरे दिमाग में) संपत्ति में किसी भी संभावित मुद्दे को रद्द करने का नियम है। –

उत्तर

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 नमूना पोस्ट नहीं किया, इसलिए मैं आप कैसे हो सकता है के रूप में एक अनुमान ले रहा हूँ इसे स्थापित किया है अगर यह मदद करता है और यह आपके लिए कैसे काम करता है, तो कृपया मुझे बताएं, क्योंकि आपका कोड व्यावहारिक दिखता है और मैं आपकी समस्या का कारण बनने के लिए उत्सुक हूं।

+2

दिलचस्प; मैं अभी तक अपने InsertItemTemplate में BindItem वाक्यविन्यास का उपयोग करने में असमर्थ था। इसने मुझे क्लाइंटस्टैटसड्रॉपडाउनलिस्ट के मेरे कार्यान्वयन पर विचार किया और फिर से संशोधन किया जहां मुझे समस्या मिली - मैं ऑनबोड (जहां! पोस्टबैक) के दौरान डेटाबिंड को कॉल कर रहा था और यही कारण है कि बिंदइटम के लिए बाध्यकारी संदर्भ अमान्य था। डेटाबिंड कॉल को हटाने से समस्या हल हो गई है। यद्यपि आपने उत्तर प्रदान नहीं किया था (जैसा कि मैंने पहले पर्याप्त प्रदान नहीं किया था), आपने मुझे इसे हल करने में मदद की है, इसलिए मैं आपको सही के रूप में चिह्नित कर रहा हूं। –

+0

सिर्फ रिकॉर्ड के लिए: क्लाइंटस्टैटसड्रॉपडाउनलिस्ट एक ड्रॉपडाउनलिस्ट का कार्यान्वयन है जो बाध्यकारी हार्ड कोड (ड्रॉपडाउनलिस्ट से व्युत्पन्न) के साथ है ताकि मैं आसानी से पेज पर एक छोड़ सकूं और तारों के बारे में चिंता न करें। –