PDA

View Full Version : [CLOSED] Razor - Getting result from DirectMethod



OriCoder
Feb 08, 2013, 4:43 PM
Hi Guys

Stuck again have an javascript function that is adding a row to a Gridpanel but I need to get a new Id from the database as part of the default data. I can get the data from the DB I want and I see the javascript firing but my 'OrderId' always comes back as 0 when the row hits the grid.

... Any ideas....

Controller Action


public ActionResult NewLeg(int OrderLinkId)
{
int id = 0;

if (ModelState.IsValid)
{
NewOrder NewOrder = new NewOrder();
int? lastOrderLinkId = (from o in db.NewOrder
select (int?)o.OrderLinkId).Max();
int newOrderLinkId = (lastOrderLinkId != null) ? (int)lastOrderLinkId + 1 : 1;

NewOrder.ExecutedTs = System.DateTime.Now;
NewOrder.CreatedTs = System.DateTime.Now;
NewOrder.OrderLinkId = newOrderLinkId;
db.NewOrder.Add(NewOrder);
db.SaveChanges();
id = NewOrder.OrderId;
}

return Json(new { OrderId = id},JsonRequestBehavior.AllowGet);
}


View Javascript function....


function NewOrderLeg_Click() {
var storeSt2 = Ext.getStore('OrderLegs');
var OrderId;

Ext.net.DirectMethod.request({
url: 'ExtNet/NewLeg?OrderLinkId=' + storeSt2.getAt(0).get('OrderLinkId'),
success: function (data) {
OrderId = data.OrderId;
}
});
var OrderLinkId = storeSt2.getAt(0).get('OrderLinkId');
var stock = storeSt2.getAt(0).get('Stock');
var nominal = storeSt2.getAt(0).get('Nominal');
var BuyOrSell = storeSt2.getAt(0).get('BuyOrSellId');
if (BuyOrSell == 1) {
BuyOrSell = 2;
} else if (BuyOrSell == 2) {
BuyOrSell = 1;
}
var defaultData = {
OrderId: OrderId,
OrderLinkId: OrderLinkId,
Stock: stock,
BuyOrSellId: BuyOrSell,
Nominal: nominal,
Price: ''
};

storeSt2.insert(0, defaultData);
};


any Ideas..

Daniil
Feb 08, 2013, 5:06 PM
Hi @OriCoder,

Please use a DirectResult or this.Direct as here:
http://mvc.ext.net/#/Events/DirectMethod/

The Direct() method applies a result object as a parameter.

Example

return this.Direct(new { data = "Hello!" });

It should be accessible within a success handler.

Example

success: function (result) {
alert(result.data);
}

OriCoder
Feb 11, 2013, 1:56 AM
Thanks all sorted now