0
我想將變量傳遞給其他控制器TableName, 我試圖ViewBag.Variable但發送null。如何將變量從一個控制器傳遞到另一個
如果你能通過從一個控制器這些參數到另一個 非常感謝你提前爲幫助
public class HomeController : Controller
{
public ActionResult Index()
{
string NombreVariable = "TablaUsuario";
ViewBag.Variable = NombreVariable;
return View();
}
[ValidateInput(false)]
public ActionResult GridViewPartial()
{
var entity = (IEnumerable)db.GetType()
.GetProperty(ViewBag.Variable)
.GetValue(db, null);
var model = entity.Cast<object>();
return 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)
{
try
{
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 i in tb.GetType().GetProperties())
{
valores[contadores] = "'" + Convert.ToString(GridViewExtension.GetEditValue<dynamic>(i.Name)) + "' AS " + i.Name ;
contadores += 1;
}
db.Database.ExecuteSqlCommand("insert into TableUsuario SELECT " + string.Join(",",valores));
db.SaveChanges();
}
catch (Exception e)
{
ViewData["EditError"] = e.Message;
}
}
else
ViewData["EditError"] = "Please, correct all errors.";
return PartialView("_GridViewPartial", model);
}
你能告訴你如何訪問你的視圖中的ViewBag –
你使用ViewBag將數據從控制器傳遞到視圖。不要將數據從控制器傳遞到控制器。 –
http://stackoverflow.com/questions/2269376/passing-information-between-controllers-in-asp-net-mvc –