FormPanel with strongly-typed aspx view

  1. #1

    FormPanel with strongly-typed aspx view

    Hi,


    I am trying to use a FormPanel with a strongly-type aspx view. The elements of my model are Time, Description, Action, and NotifyTime. Here is the view so far:


    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<EMIAQToolkit.EventLog>" %>
    
    <%@ Import Namespace="System.Collections.Generic"%>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <!DOCTYPE html>
    
    <html>
    <head id="Head1" runat="server">
    <title> Add event </title>
    
    <body>
    
    <form id="Form1" runat="server">
        <ext:ResourceManager ID="ResourceManager1" runat="server" />
    
          <ext:FormPanel 
                ID="UserForm" 
                runat="server"
                Frame="true"
                LabelAlign="Right"
                Title="Add Event -- Alert message and notify date are optional"
                Width="500"
                >
                <Items>
                    <ext:DateField ID="Datefield1" runat="server"
                        FieldLabel="Date"
                        Name="Time"
                        AllowBlank="false"
                        Vtype="Date"
                        AnchorHorizontal="100%" AutoDataBind="true"
                        />
                    
                    <ext:TextField ID="TextField1" runat="server"
                        FieldLabel="Description"
                        Name="Description"
                        AllowBlank="false"
                        AnchorHorizontal="100%" AutoDataBind="true"
                        />
                    
                    <ext:ComboBox ID="ComboBox1" runat="server"
                                FieldLabel="Set Alert"
                                Name="Action"
                                TypeAhead="true"
                                SelectOnTab="true" AutoDataBind="true">
                                <Items>
                                    <ext:ListItem Text="No Alert" />
                                    <ext:ListItem Text="Calibrate" />
                                    <ext:ListItem Text="Replace" />
                                    <ext:ListItem Text="Check" />
                                </Items>
                            </ext:ComboBox>
                      <ext:DateField ID="Datefield2" runat="server"
                        FieldLabel="Notify Date"
                        Name="NotifyTime"
                        AllowBlank="true"
                        Vtype="Date"
                        AnchorHorizontal="100%" AutoDataBind="true"
                        />
                </Items>            
                
                <Buttons>
                    <ext:Button ID="Button1" 
                        runat="server"
                        Text="Save"
                        Icon="Disk"
                        AutoPostBack="true" 
                        />
                    <ext:Button ID="Button2" 
                        runat="server"
                        Text="Reset">
                        <Listeners>
                            <Click Handler="#{UserForm}.getForm().reset();" />
                        </Listeners>
                    </ext:Button>
                </Buttons>
            </ext:FormPanel>
     </form>
    </body>
    </html>
    Is there a tag that could be placed in the DateField/TextField/ComboBox elements. I already placed the AutoDataBinding tag but this isn't working.
    On postback the model is getting the time="01/01/1901" and everything else empty.

    Thanks,

    Eli
  2. #2
    FormPanel doesn't render HTML form tag
    Wrap the form panel by form tags
  3. #3
    I do have the FormPanel wrapped by form tags
  4. #4
    How and wher do you check values?
  5. #5
    I'm checking values by looking at the tables in the database.
    Here is the Controller:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Ext.Net.MVC;
    using System.ComponentModel;
    
    namespace EMIAQToolkit.Models
    {
        public class EventController : System.Web.Mvc.Controller
        {
           
            EventRepository eventRepository = new EventRepository();
    
            //
            // GET: /Event/Add/2
    
            public ActionResult Add(int id)
            {
                EventLog eventLog = new EventLog()
                {
                    TrendId_FK = (short)id,
                    Time = DateTime.Now
                };
                return View(eventLog);
    
            }
    
            //
            // POST: /Event/Add/2
    
            [HttpPost]
            public ActionResult Add(int id, FormCollection collection)
            {
                EventLog eventLog = new EventLog();
                eventLog.TrendId_FK = (short)id;
                eventRepository.Add(eventLog);
                eventRepository.Save();
    
                return RedirectToAction("Add", new { id = eventLog.TrendId_FK });
             
            }
                
        }
    }
    Here is the EventRepository Class:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace EMIAQToolkit.Models
    {
        public class EventRepository
        {
            private ACEMIAQ_DEVEntities dba = new ACEMIAQ_DEVEntities();
    
            public IQueryable<EventLog> GetAllEvents()
            {
                return dba.EventLogs;
            }
    
            public IQueryable<EventLog> FindEvents(int trendId)
            {
                return from eventlog in GetAllEvents()
                       where eventlog.TrendId_FK == trendId
                       select eventlog;
            }
    
            public void Add(EventLog eventLog)
            {
                dba.EventLogs.AddObject(eventLog);
            }
    
            public void Save()
            {
                dba.SaveChanges();
            }
    
        }
      
    }
  6. #6
    AutoPostBack is WebForm functuonality and should not be used under MVC
    Use the following Handler for the Button
    Handler="this.el.up('form').dom.submit();"
  7. #7
    Ok I took off the autopostback and put that instead but this still isn't binding my model to the values in the text boxes. Is there a way to bind the model with the text boxes based on the Name property of the TextField tag?
  8. #8
    Model binding is supported since 2.1 version
    2.1 version is not released yet (available in SVN only)

    2.1 version will be released with MVC Examples Explorer to demonstrate supported MVC functionality
  9. #9
    I have gotten it to work. I went back to just a regular form using the html helpers and I was still getting an out of range date for the Time field and nothing else filled in. I remembered that I had modified the EventController to not handle validation because I was going to disable the save button if the form wasn't valid. I have reverted to the validation in the controller and now it works.

    [HttpPost]
            public ActionResult Add(int id, FormCollection collection)
            {
                EventLog eventLog = new EventLog();
    
                var actions = new[] { "No Alert", "Calibrate", "Replace", "Check" };
                ViewData["Actions"] = new SelectList(actions, eventLog.Action);
    
                if (TryUpdateModel(eventLog))
                {
    
                    if (eventLog.Action == "No Alert")
                    {
                        eventLog.Action = null;
                    }
    
                    eventLog.TrendId_FK = (short)id;
                    eventRepository.Add(eventLog);
                    eventRepository.Save();
    
                    return RedirectToAction("Add", new { id = eventLog.TrendId_FK });
                }
    
                eventLog.TrendId_FK = (short)id;
    
                return View(eventLog);
            }
    and here is the Add.aspx page

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<EMIAQToolkit.EventLog>" %>
    
    <%@ Import Namespace="System.Collections.Generic"%>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    
        
    <!DOCTYPE html>
    
    <html>
    <head id="Head1" runat="server">
        <title>Add Event</title>
        <link href="/content/styling.css" rel="stylesheet" type="text/css" />    
    </head>
    <body>
    
        <form id="Form1" runat="server">
        <ext:ResourceManager ID="ResourceManager1" runat="server" />
    
          <ext:FormPanel 
                ID="UserForm" 
                runat="server"
                Frame="true"
                LabelAlign="Right"
                Title="Add Event -- Alert message and notify date are optional"
                Width="500"
                >
                <Items>
                    <ext:DateField ID="Datefield1" runat="server"
                        FieldLabel="Date"
                        Name="Time"
                        AllowBlank="false"
                        Vtype="Date"
                        AnchorHorizontal="100%"
                        />
                    
                    <ext:TextField ID="TextField1" runat="server"
                        FieldLabel="Description"
                        Name="Description"
                        AllowBlank="false"
                        AnchorHorizontal="100%"
                        />
                    
                    <ext:ComboBox ID="ComboBox1" runat="server"
                                FieldLabel="Set Alert"
                                Name="Action"
                                TypeAhead="true"
                                SelectOnTab="true">
                                <Items>
                                    <ext:ListItem Text="Shade" />
                                    <ext:ListItem Text="Mostly Shady" />
                                    <ext:ListItem Text="Sun or Shade" />
                                    <ext:ListItem Text="Mostly Sunny" />
                                    <ext:ListItem Text="Sunny" />
                                </Items>
                            </ext:ComboBox>
                      <ext:DateField ID="Datefield2" runat="server"
                        FieldLabel="Notify Date"
                        Name="NotifyTime"
                        AllowBlank="true"
                        Vtype="Date"
                        AnchorHorizontal="100%"
                        />
                </Items>            
                
                <Buttons>
                    <ext:Button ID="Button1" 
                        runat="server"
                        Text="Save"
                        Icon="Disk" FormBind="true">
                            <Listeners>
                                <Click Handler="this.el.up('form').dom.submit();" />
                            </Listeners>
                        </ext:Button>
                    <ext:Button ID="Button2" 
                        runat="server"
                        Text="Reset">
                        <Listeners>
                            <Click Handler="#{UserForm}.getForm().reset();" />
                        </Listeners>
                    </ext:Button>
                </Buttons>
            </ext:FormPanel>
        </form>
    </body>
    </html>
    I've set FormBind="true" for Button1 in order to make it where the button isn't enabled whenever the form is invalid but the button never becomes enabled when the form becomes valid. Am I using this property correctly?

    Also, in IE in debugging mode, when I click on the calender button to input a value for the Date or for the Notify date there is an error:
    Error: Object doesn't support property or method 'Date'

Similar Threads

  1. MVC Add Controller / Strongly Typed View Bug
    By Doug.Morrow in forum 2.x Help
    Replies: 0
    Last Post: Aug 08, 2012, 10:03 PM
  2. Replies: 1
    Last Post: Jan 09, 2012, 10:55 AM
  3. Replies: 0
    Last Post: Nov 24, 2011, 12:30 PM
  4. [CLOSED] httpproxy/AjaxStoreResult in Strongly typed View
    By GLD in forum 1.x Legacy Premium Help
    Replies: 4
    Last Post: May 17, 2011, 2:43 PM
  5. Replies: 2
    Last Post: Oct 19, 2008, 11:48 PM

Tags for this Thread

Posting Permissions