简体   繁体   English

将ViewBag从Controller传递到部分视图

[英]Pass ViewBag from Controller to Partial view

My controller action is 我的控制器动作是

public ActionResult Generate(int? id, int? kol)
{
    string name = db.Sifres.Where(c => c.id == id).Select(c => c.name).FirstOrDefault();

    ViewBag.Sif = name;

    return PartialView("_GenerateNumbers");
}

Through debugger and breakpoints I can see that variable name and ViewBag.Sif have values. 通过调试器和断点,我可以看到变量nameViewBag.Sif具有值。 When the controller returns partial view the values of variable name and ViewBag.Sif are null ! 当控制器返回部分视图时,变量nameViewBag.Sif值为null Why? 为什么? Why those loose their values? 为什么那些人失去了价值观?

通常对于部分视图,我向它发送一个包含我想要的所有数据的模型,但是如果您确实只需要发送此字符串,则只需显式发送即可:

return PartialView("_GenerateNumbers", name);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM