PDA

View Full Version : [CLOSED] Razor MVC formpanel example



OriCoder
Apr 01, 2013, 10:13 PM
Hi guys

Is there a good razor example of saving a "multi" FormPanel back using a DirectEvent like a grid panel e.g.



.DirectEvents(de =>
{
de.Click.Url = Url.Action("HandleChanges");
de.Click.ExtraParams.Add(new Parameter
{
Name = "data",
Value = "this.up('grid').store.getChangedData({skipIdForNew Records : false})",
Mode = ParameterMode.Raw,
Encode = true
});


Just not sure where to start unless I do all the fields one by one which is not very practical.

Baidaly
Apr 01, 2013, 10:28 PM
Hello!

Sorry, I don't quite understand your problem. You want to send GridPanel values to the server or do something with FormPanel?

OriCoder
Apr 01, 2013, 10:49 PM
Hello!

Sorry, I don't quite understand your problem. You want to send GridPanel values to the server or do something with FormPanel?

I am populating a formpanel and need to save all the fields back to the database, dont want to have to put all the values in a store then pass back. Just want to pass the whole formpanel to the controller via a direct event.

Baidaly
Apr 01, 2013, 11:12 PM
Try the following example:

Index.cshtml


@(X.FormPanel()
.Title("Company data")
.Width(800)
.BodyPadding(5)
.Layout(LayoutType.Column)
.FieldDefaults(fd => {
fd.LabelAlign = LabelAlign.Left;
fd.MsgTarget = MessageTarget.Side;
})
.Items(
X.GridPanel()
.Title("Company Data")
.ColumnWidth(0.6)
.Height(400)
.Store(X.Store()
.ID("Store1")
.Data(Model)
.Reader(new ArrayReader())
.Model(X.Model()
.Fields(f => {
f.Add(X.ModelField().Name("company"));
f.Add(X.ModelField().Name("price").Type(ModelFieldType.Float));
f.Add(X.ModelField().Name("change").Type(ModelFieldType.Float));
f.Add(X.ModelField().Name("pctChange").Type(ModelFieldType.Float));
f.Add(X.ModelField().Name("lastChange").Type(ModelFieldType.Date).DateFormat("M/d hh:mmtt"));
f.Add(X.ModelField().Name("rating").Type(ModelFieldType.Int).Convert(j => j.Handler = "var pct = record.get('pctChange'); if (pct < 0) return 2; if (pct < 1) return 1; return 0;"));
})
)
)
.ColumnModel(
X.Column().Text("Company").DataIndex("company").Flex(1),
X.Column().Text("Price").Width(75).DataIndex("price").Renderer(RendererFormat.UsMoney),
X.Column().Text("Change").Width(75).DataIndex("change"),
X.Column().Text("Change").Width(75).DataIndex("pctChange"),
X.DateColumn().Text("Last Updated").Width(85).DataIndex("lastChange"),
X.Column().Text("Rating").Width(30).Sortable(true).DataIndex("rating")
)
.Listeners(l => l.SelectionChange.Handler = "if (selected[0]) { this.up('form').getForm().loadRecord(selected[0]); }"),

X.FieldSet()
.ColumnWidth(0.4)
.Title("Company details")
.MarginSpec("0 0 0 10")
.Defaults(d => {
d.Add(new Parameter("Width", "240"));
d.Add(new Parameter("LabelWidth", "90"));
})
.Items(
X.TextField()
.Name("company")
.FieldLabel("Name"),
X.TextField()
.Name("price")
.FieldLabel("Price"),
X.TextField()
.Name("pctChange")
.FieldLabel("Change (%)"),
X.DateField()
.Name("lastChange")
.FieldLabel("Last Updated"),
X.RadioGroup()
.FieldLabel("Rating")
.ColumnsNumber(3)
.AutomaticGrouping(false)
.Items(
X.Radio().Name("rating").InputValue("0").BoxLabel("A"),
X.Radio().Name("rating").InputValue("1").BoxLabel("B"),
X.Radio().Name("rating").InputValue("2").BoxLabel("C")
)
)
)
.Buttons(
X.Button()
.Text("Submit")
.DirectClickAction("Submit"),
X.Button()
.Text("Save To Grid")
.OnClientClick("var form = this.up('form'); form.getForm().updateRecord(form.down('grid').getS electionModel().getLastSelected());")
)
)


HomeController:



public class HomeController : Controller
{
public ActionResult Index()
{
return View(GetData());
}
public ActionResult Submit(string company, string lastChange, string pctChange, double price, int rating)
{
X.Msg.Alert("Company", company).Show();
return this.Direct();
}

private object[] GetData()
{
return new object[]
{
new object[] { "3m Co", 71.72, 0.02, 0.03, "9/1 12:00am" },
new object[] { "Alcoa Inc", 29.01, 0.42, 1.47, "9/1 12:00am" },
new object[] { "Altria Group Inc", 83.81, 0.28, 0.34, "9/1 12:00am" }
};
}

}

OriCoder
Apr 01, 2013, 11:23 PM
This is what I want to avoid, you are having to catch all the form elements. Is there no object that can be passed to the controller for a formpanel?

Also the direct event will be triggered from a button not on the formpanel, but a toolbar at the side.

Baidaly
Apr 01, 2013, 11:35 PM
This is what I want to avoid, you are having to catch all the form elements. Is there no object that can be passed to the controller for a formpanel?

Please, take a look at the following example: http://mvc.ext.net/#/Models/Submit/



Also the direct event will be triggered from a button not on the formpanel, but a toolbar at the side.


Pay attention on FormID parameter

OriCoder
Apr 02, 2013, 11:19 AM
Thanks that should get me started!