PDA

View Full Version : [CLOSED] reload data of grid after save a new record MVC



matrixwebtech
Jun 10, 2014, 8:17 PM
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_MasterManagemen t_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

Daniil
Jun 11, 2014, 6:32 AM
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.
http://examples2.ext.net/#/GridPanel/Saving_Variations/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());
}

matrixwebtech
Jun 11, 2014, 10:24 AM
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.
http://examples2.ext.net/#/GridPanel/Saving_Variations/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"))
)

Daniil
Jun 11, 2014, 3:01 PM
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.

sundarsureshin
Jul 10, 2014, 7:10 AM
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>

Daniil
Jul 10, 2014, 1:12 PM
Please clarify how do you reload the View?

sundarsureshin
Jul 11, 2014, 6:08 AM
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) )

Daniil
Jul 11, 2014, 5:02 PM
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.