[CLOSED] reload data of grid after save a new record MVC

  1. #1

    [CLOSED] reload data of grid after save a new record MVC

    My Grid Panel

     Html.X().GridPanel()
    .ID("GridPanel1")
    .Title("Tasks")
    .Frame(true)
    .Layout("fit")
    .Store(
    Html.X().Store()
    .ID("Store1")
    .Model(
    Html.X().Model()
     .IDProperty("MaterialCatergoryID")
    .Fields(
    
    Html.X().ModelField().Name("IsActive").Type(ModelFieldType.Boolean),
    
    Html.X().ModelField().Name("CategoryName").Type(ModelFieldType.String)
    
    )
    )
     .DataSource(ViewBag.loadgrid)
    .PageSize(10)
    )
    .ColumnModel(
     Html.X().Column()
    .DataIndex("IsActive")
    .Text("IsActive")
    .Flex(1),
     Html.X().Column()
    .DataIndex("CategoryName")
    .Text("CategoryName")
    .Flex(1)
    )
     .SelectionModel(
    X.CheckboxSelectionModel()
    .ID("s")
    .Mode(SelectionMode.Multi)
    )
    .Plugins(
        Html.X().CellEditing()
    )
    .BottomBar(
    Html.X().PagingToolbar()
    .HideRefresh(true)
    )
    .TopBar(
    Html.X().Toolbar()
    .Items(
    Html.X().TextField()
    .ID("txtSearch")
    .EmptyText("Search by Module name")
    .DirectEvents(de =>
    {
        de.Change.Action = "/search";
        de.Change.ExtraParams.Add(new Parameter
        {
            Name = "data",
            Value = "Ext.getCmp('txtSearch').getValue()",
            Mode = ParameterMode.Value,
            Encode = false
        }
        );
    })

    Controller Code
    public ActionResult Index()
            {
                
                ViewBag.loadgrid = GetData();
                
    
                return View(Views.view.PurchaseManagement_MasterManagement_MaterialCategory_Index);
            }
    
    private List<tbl_Pur_Master_MaterialCategory> GetData()
            {
                _UserSession = (UserSession)Session[AppSession.SessionCurrentUser];
                List<tbl_Pur_Master_MaterialCategory> tbl_Pur_Master_MaterialCategory = new List<tbl_Pur_Master_MaterialCategory>();
                tbl_Pur_Master_MaterialCategory obj_tbl_Pur_Master_MaterialCategory = new tbl_Pur_Master_MaterialCategory();
                obj_tbl_Pur_Master_MaterialCategory.CompanyID = _UserSession.CompanyID;
    
                tbl_Pur_Master_MaterialCategory = _MaterialCategory.pur_master_materialcategory_View(obj_tbl_Pur_Master_MaterialCategory);
    
                return tbl_Pur_Master_MaterialCategory;
    
            }
    live example here
    http://legalinfoservices.com/home
    Purchase Management->Master Management->Material Category
    Last edited by Daniil; Jun 17, 2014 at 11:41 AM. Reason: [CLOSED]
  2. #2
    Hi @matrixwebtech,

    I don't see how you save a new record.

    Generally speaking, a regular scenario doesn't require reloading. Please try to insert a record and save in this example.
    https://examples2.ext.net/#/GridPane...s/HttpHandler/

    So, I would recommend you to avoid reloading if possible. Why to do an excessive request?

    Though, answering your question how to reload your Store, I would recommend to use a Store's ServerProxy. Here is an example.

    View
    @{
        var X = Html.X(); 
    }
    
    <!DOCTYPE html>
    <html>
    <head>
        <title>Ext.Net.MVC v2 Example</title>
    </head>
    <body>
        @X.ResourceManager()
    
        @X.Button().Text("Reload").Handler("App.GridPanel1.getStore().reload();")
    
        @(X.GridPanel()
            .ID("GridPanel1")
            .Height(200)
            .Store(X.Store()
                .Model(X.Model().Fields("test"))
                
                .ServerProxy(X.AjaxProxy()
                    .Url(Url.Action("GetData"))
                    .Reader(X.JsonReader().Root("data"))
                )
                
                .DataSource(ViewBag.Data)
            )
            .ColumnModel(
                X.Column()
                    .Text("Test")
                    .DataIndex("test")
            )
        )
    </body>
    </html>
    Controller
    public object[] CreateTestData()
    {
        int s = DateTime.Now.Second;
     
        return new object[]
        {
            new 
            {
                test = "Test " + s
            },
            new 
            {
                test = "Test " + s
            }
        };
    }
    
    public ActionResult Index()
    {
        ViewBag.Data = this.CreateTestData();
    
        return View();
    }
    
    public ActionResult GetData()
    {
        return this.Store(this.CreateTestData());
    }
  3. #3
    Quote Originally Posted by Daniil View Post
    Hi @matrixwebtech,

    I don't see how you save a new record.

    Generally speaking, a regular scenario doesn't require reloading. Please try to insert a record and save in this example.
    https://examples2.ext.net/#/GridPane...s/HttpHandler/

    So, I would recommend you to avoid reloading if possible. Why to do an excessive request?

    Though, answering your question how to reload your Store, I would recommend to use a Store's ServerProxy. Here is an example.

    View
    @{
        var X = Html.X(); 
    }
    
    <!DOCTYPE html>
    <html>
    <head>
        <title>Ext.Net.MVC v2 Example</title>
    </head>
    <body>
        @X.ResourceManager()
    
        @X.Button().Text("Reload").Handler("App.GridPanel1.getStore().reload();")
    
        @(X.GridPanel()
            .ID("GridPanel1")
            .Height(200)
            .Store(X.Store()
                .Model(X.Model().Fields("test"))
                
                .ServerProxy(X.AjaxProxy()
                    .Url(Url.Action("GetData"))
                    .Reader(X.JsonReader().Root("data"))
                )
                
                .DataSource(ViewBag.Data)
            )
            .ColumnModel(
                X.Column()
                    .Text("Test")
                    .DataIndex("test")
            )
        )
    </body>
    </html>
    Controller
    public object[] CreateTestData()
    {
        int s = DateTime.Now.Second;
     
        return new object[]
        {
            new 
            {
                test = "Test " + s
            },
            new 
            {
                test = "Test " + s
            }
        };
    }
    
    public ActionResult Index()
    {
        ViewBag.Data = this.CreateTestData();
    
        return View();
    }
    
    public ActionResult GetData()
    {
        return this.Store(this.CreateTestData());
    }
    Thanks for Reply,
    Is there any way to reload grid without using

    Handler("App.GridPanel1.getStore().reload();
    .ServerProxy(X.AjaxProxy()
                    .Url(Url.Action("GetData"))
                    .Reader(X.JsonReader().Root("data"))
                )
  4. #4
    In other words you need this line to be re-executed?
    .DataSource(ViewBag.loadgrid)
    I think it is only possible via reloading the entire view.
  5. #5
    its not working . i have written all my sample code with this thread

    my model is



    public class Samplelist
        {
            [Key]
            public int POID { get; set; }
            public string PO { get; set; }
            public DateTime PODATE { get; set; }
            public Decimal AmountSGD { get; set; }
    
            public static IEnumerable<Samplelist> GetAll()
            {
                DbFactory ObjDbfactory = new DbFactory(DataBaseType.SQLServer, ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
                DataSet ds = ObjDbfactory.GetData("SP_SelectList", false);
                ObjDbfactory.CloseConnection();
                List<Samplelist> Samplelist = new List<Samplelist>();
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    Samplelist.Add(new Samplelist { POID = Convert.ToInt32(dr["POID"]), PO = Convert.ToString(dr["PO"]), AmountSGD = Convert.ToDecimal(dr["AmountSGD"]), PODATE = Convert.ToDateTime(dr["PODATE"]) });
                }
               
                return Samplelist;
            }
        }


    My Controller is


    DbFactory ObjDbfactory;
          
            protected void InitializeDB()
            {
                ObjDbfactory = new DbFactory(DataBaseType.SQLServer, ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
            }
    
            public ActionResult Index()
            {
                ViewBag.Data =Samplelist.GetAll();
                return View();
            }
            [HttpPost]
            public ActionResult Index(String PODATE, string POREF, string POAMOUNT)
            {
                string[] CustDate = PODATE.ToString().Split('T');
                string custdatere = CustDate[0].ToString().Replace('\\', ' ');
                DateTime dt = Convert.ToDateTime(custdatere.ToString().Replace('"', ' ').Trim()); 
                
                //inserting data into the database
    
                InitializeDB();        
                List<DbParams> collection = new List<DbParams>();
                collection.Add(new DbParams(DbType.String, 50, POREF, "@Poref", ParameterDirection.Input));
                collection.Add(new DbParams(DbType.Date, 50, dt, "@Podate", ParameterDirection.Input));
                collection.Add(new DbParams(DbType.Decimal, 50, POAMOUNT, "@Poamount", ParameterDirection.Input));
                DataSet ds = ObjDbfactory.GetData("SP_InsertPO", false, collection);
                ObjDbfactory.CloseConnection(); 
    
                //fetching data from the database
                             
                ViewBag.Data = Samplelist.GetAll();
    
                // how to ReBind this  to the grid its not working 
               
                return this.View(ViewBag.Data);
            }
            public ActionResult Read()
            {
                var properties = Samplelist.GetAll();
                return this.Store(Samplelist.GetAll());
            }
    And my View is



    @{
        Layout = null;
        var X = Html.X();
    }
    
    <form>
            @Html.X().ResourceManager()
            <div>
    
                            
                                         
                                                       
                                                        @(X.DateField()
                                                        .ID("txtDate")
                                                        .Format("dd/MM/yyyy")
                                                        )
                                                    
                                                        
                                                        @(X.TextField()
                                                        .ID("txtPOREF"))
    
                                                        
                                                        @(X.TextField()
                                                         .ID("txtAmount"))
                                                 
                                                                
                                                                    @( Html.X().Button()
                                                                            .Text("ADD")
                                                                            
                                                                            .DirectEvents(de =>
                                                                            {
                                                                                de.Click.Url = Url.Action("Index");
                                                                                de.Click.EventMask.ShowMask = true;
    
                                                                                de.Click.ExtraParams.Add(new Parameter("PODATE", "#{txtDate}.getValue()", ParameterMode.Raw));
                                                                                de.Click.ExtraParams.Add(new Parameter("POREF", "#{txtPOREF}.getValue()", ParameterMode.Raw));
                                                                                de.Click.ExtraParams.Add(new Parameter("POAMOUNT", "#{txtAmount}.getValue()", ParameterMode.Raw));
                                                                                                 
                                                                            }))
                                                
    
    
                                                        
                                                       
                                                        @(Html.X().GridPanel()
                                                            .Title("Editable GridPanel")
                                                            .Width(600)
                                                            .Height(350)
                                                            .ID("editgrid")
                                                            .AutoDataBind(true)
                                                            .Header(true)
                                                            
                                                            .Store(Html.X().Store()
                                                                .ID("Store1")
                                                               
                                                                
                                                                .Model(Html.X().Model()
                                                                   
                                                                    .Fields(
                                                                //new ModelField("ID", ModelFieldType.String),
                                                                    new ModelField("PO", ModelFieldType.String),
                                                                    new ModelField("PODATE", ModelFieldType.Date),
                                                                    new ModelField("AmountSGD", ModelFieldType.Float),
                                                                    new ModelField("POID", ModelFieldType.Int)
                                                                    )
                                                                )
                                                                
                                                               .DataSource(ViewBag.Data)
                                                           
                                                                .ServerProxy(
                                                                     Html.X().AjaxProxy()
                                                                     .Url(Url.Action("Read"))
                                                                    .Reader(Html.X().JsonReader().Root("data"))
                                                                )
    
                                                            )
                                                            .ColumnModel(
                                                                //Html.X().Column().Text("ID").DataIndex("ID").Width(35).Editor(Html.X().TextField()),
                                                                Html.X().Column().Text("POID").DataIndex("POID").Width(35),
                                                                Html.X().Column()
                                                                    .Text("PO")
                                                                    .DataIndex("PO")
                                                                    .Flex(1)
                                                                    .Editor(Html.X().TextField()),
    
    
                                                                 Html.X().Column()
                                                                    .Text("AmountSGD")
                                                                    .DataIndex("AmountSGD")
    
                                                                    .Editor(Html.X().TextField()),
    
                                                                Html.X().DateColumn()
                                                                    .Text("PODATE")
                                                                    .DataIndex("PODATE")
                                                                    .Format("yyyy-MM-dd")
                                                                    .Editor(Html.X().DateField().Format("yyyy-MM-dd"))
    
                                                            )
                                                            .SelectionModel(Html.X().CellSelectionModel())
                                                            .Plugins(
                                                                Html.X().CellEditing().Listeners(ls => ls.Edit.Fn = "edit")
                                                            )
                                                            )
                <script>
                    var template = 'color:{0};';
    
                    var change = function (value, meta) {
                        meta.style = Ext.String.format(template, (value > 0) ? "green" : "red");
                        return value;
                    };
    
                    var pctChange = function (value, meta) {
                        meta.style = Ext.String.format(template, (value > 0) ? "green" : "red");
                        return value + "%";
                    };
    
                    var edit = function (editor, e) {
                        alert("hai");
                        /*
                            "e" is an edit event with the following properties:
                
                                grid - The grid
                                record - The record that was edited
                                field - The field name that was edited
                                value - The value being set
                                originalValue - The original value for the field, before the edit.
                                row - The grid table row
                                column - The grid Column defining the column that was edited.
                                rowIdx - The row index that was edited
                                colIdx - The column index that was edited
                        */
    
                        // Call DirectMethod
                        if (!(e.value === e.originalValue || (Ext.isDate(e.value) && Ext.Date.isEqual(e.value, e.originalValue)))) {
                            Ext.net.DirectMethod.request({
                                url: '@(Url.Action("Edit"))',
                    params: {
                        id: e.record.data.POID,
                        field: e.field,
                        oldValue: e.originalValue,
                        newValue: e.value,
                        customer: e.record.data
                    }
                });
            }
        };
    </script>
                                              
                                   
                
                    
            </div>
            </form>
    Last edited by Daniil; Jul 10, 2014 at 10:29 AM. Reason: Please use [CODE] tags
  6. #6
    Please clarify how do you reload the View?
  7. #7
    i am not reloading the view, iam redirecting to the view in buttonadd ...At that time i want my grid model to be reloaded . Please see my earlier post of my sample ...Controller ( HttpPost (Index) )
  8. #8
    You cannot return a .View() from a DirectEvent handler. You can .Redirect().

    View
    @{
        var X = Html.X(); 
    }
    
    <!DOCTYPE html>
    <html>
    <head>
        <title>Ext.Net.MVC v2 Example</title>  
    </head>
    <body>
        @X.ResourceManager()
        
        @(X.GridPanel()
            .Store(X.Store()
                .Model(X.Model().Fields("test"))
                .DataSource(ViewBag.Data)
            )
            .ColumnModel(
                X.Column().Text("Test").DataIndex("test")
            )
        ) 
    
        @(X.Button().Text("Reload").DirectClickAction("Reload"))
    </body>
    </html>
    Controller
    public ActionResult Index()
    {
        if (TempData["Data"] != null)
        {
            ViewBag.Data = TempData["Data"];
        }
        else
        {
            ViewBag.Data = new object[]
            {
                new
                {
                    test = "initial"
                }
            };
        }
    
        return View();
    }
    
    public ActionResult Reload()
    {
        object[] data = new object[]
        {
            new
            {
                test = "new"
            }
        };
    
        TempData["Data"] = data;
    
        return this.Redirect("Index");
    }
    Though, I don't think a View should be reload at all just to reload a Store. I would still recommend to use a ServerProxy.

Similar Threads

  1. Can I save and after that Reload the filters state in Ext.NET Grid view
    By Nhím Hổ Báo in forum 1.x Help
    Replies: 14
    Last Post: Mar 26, 2015, 10:22 AM
  2. Replies: 0
    Last Post: Oct 24, 2011, 4:26 PM
  3. [CLOSED] Grid: save new Record: property missing
    By pschojer in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Aug 19, 2009, 9:45 AM
  4. grid save and reload
    By [WP]joju in forum 1.x Help
    Replies: 2
    Last Post: Aug 10, 2009, 4:18 AM
  5. [CLOSED] Save Data in grid, with StoreProcedure
    By Jurke in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Jan 28, 2009, 4:00 AM

Tags for this Thread

Posting Permissions