Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 局部视图的MVC ViewBag为空_Asp.net Mvc - Fatal编程技术网

Asp.net mvc 局部视图的MVC ViewBag为空

Asp.net mvc 局部视图的MVC ViewBag为空,asp.net-mvc,Asp.net Mvc,有一个关于局部视图的基本问题。@ViewBag.Message在最终渲染中显示为空 我的控制器看起来像这样 public ActionResult Message() { ViewBag.Message = "This is a partial view."; return PartialView(); } public ActionResult PartialViewDemo() { return View();

有一个关于局部视图的基本问题。@ViewBag.Message在最终渲染中显示为空

我的控制器看起来像这样

    public ActionResult Message() {
        ViewBag.Message = "This is a partial view.";
        return PartialView();
    }

    public ActionResult PartialViewDemo() {
        return View();
    }
我的部分视图在Message.cshtml中如下所示

<h2>@ViewBag.Message</h2>
@ViewBag.Message
我从partialViewTest.cshtml调用这样的部分视图。@ViewBag.Message为空,仅显示h2标记

<div>@Html.Partial("~/Views/Sample/Message.cshtml")</div>
@Html.Partial(“~/Views/Sample/Message.cshtml”)
如果我使用javascript实现,效果会很好

<div id="result"></div>

@section scripts {
    <script type="text/javascript">
        $(function () {
            $('#result').load('/sample/message');
        });
    </script>
}

@节脚本{
$(函数(){
$('#result')。加载('/sample/message');
});
}

只能从partialViewTest.cshtml中呈现Message.cshtml(而不是controller)。在控制器中设置ViewBag.Message时,必须像这样调用控制器(以获得部分视图)

@Html.Action("Message", "Sample")