Monday, 15 August 2011

asp.net mvc - How to pass variables from one controller to another -



asp.net mvc - How to pass variables from one controller to another -

i pass variables other controllers tablename, i'm trying viewbag.variable send null.

if can pass these parameters 1 controller give thanks much in advance help

public class homecontroller : controller { public actionresult index() { string nombrevariable = "tablausuario"; viewbag.variable = nombrevariable; homecoming view(); } [validateinput(false)] public actionresult gridviewpartial() { var entity = (ienumerable)db.gettype() .getproperty(viewbag.variable) .getvalue(db, null); var model = entity.cast<object>(); homecoming partialview("_gridviewpartial", model); } [httppost, validateinput(false)] public actionresult gridviewpartialaddnew() { var entity = (dynamic)db.gettype() .getproperty(viewbag.variable) .getvalue(db, null); var model = entity; if (modelstate.isvalid) { seek { model.tableusuario tb = new model.tableusuario(); int contador=tb.gettype().getproperties().count(); //coleccion[] cual = new coleccion[contador]; int contadores = 0; string[] valores = new string[contador]; foreach( var in tb.gettype().getproperties()) { valores[contadores] = "'" + convert.tostring(gridviewextension.geteditvalue<dynamic>(i.name)) + "' " + i.name ; contadores += 1; } db.database.executesqlcommand("insert tableusuario select " + string.join(",",valores)); db.savechanges(); } grab (exception e) { viewdata["editerror"] = e.message; } } else viewdata["editerror"] = "please, right errors."; homecoming partialview("_gridviewpartial", model); }

if want phone call method_a in controller_a, controller_b, can this:

in controller_b

controller_a mycontrollera = new controller_a(); mycontrollera.method_a(myvariable);

in controller_a

public void method_a(myclass myvariable) ... ...

alternatively, if want "global" variable available everywhere, can assign session, in similar way utilize viewbag

session["myvariable"] = myvariable;

though caution against using session: not belongs in session state.

i guess depends on want achieve.

asp.net-mvc dynamic controllers

No comments:

Post a Comment