MVC局部视图无法渲染 [英] MVC Partial view not rendering
问题描述
我是MVC的新手,我正在尝试向主布局页面添加部分视图,以便可以在整个应用程序中显示消息.我在解决它时遇到了一些麻烦:
I'm new to MVC and I'm trying to add a partial view to my main layout page so I can show messages throughout my app. I'm having some trouble woring it out:
这是我的布局:
<div class="span12">
<p>
@{Html.RenderAction("Messaging", "Messaging");}
</p>
@RenderBody()
</div>
这是我的消息传递控制器:
This is my messaging controller:
public ActionResult Messaging()
{
return PartialView(new ViewModels.Messaging()
{
MessageType = Utilities.MessageType.Success,
MessageHeader = "Test",
Message = "this is a test message"
});
}
这是共享文件夹中存储的名为_MessagingPartial.cshtml的局部视图:
Here is my partial view called _MessagingPartial.cshtml stored in the shared folder:
@model AWS.PL.ViewModels.Messaging
<span>
Model.Message
</span>
这是我的ViewModel:
And here is my ViewModel:
public class Messaging
{
public Utilities.MessageType MessageType { get; set; }
public string MessageHeader { get; set; }
public string Message { get; set; }
}
我收到错误未找到部分视图消息"错误.应该将部分视图称为Messanging.cshtml,还是我从根本上错了?
I get the error "Partial view Messaging was not found" error. Should the partial view be called Messanging.cshtml or I'm I get the something fundamentally wrong?
非常感谢任何帮助.
谢谢,威尔基.
推荐答案
这是我的局部视图,称为_MessagingPartial.cshtml
Here is my partial view called _MessagingPartial.cshtml
您的局部视图应称为 Messaging.cshtml
coz,这是呈现该视图的控制器操作的名称.
Your partial view should be called Messaging.cshtml
coz that's the name of the controller action that rendered it.
如果要呈现自定义的部分名称,请确保您明确指定:
If you want to render a custom partial name, make sure you explicitly specify that:
public ActionResult Messaging()
{
var model = new ViewModels.Messaging
{
MessageType = Utilities.MessageType.Success,
MessageHeader = "Test",
Message = "this is a test message"
};
return PartialView("_MessagingPartial", model);
}
,如果局部文件位于某些非标准位置,则您也可以指定其完整路径:
and if the partial is located in some non-standard location you could also specify the full path to it:
return PartialView("~/Views/FooBar/Baz/_MessagingPartial.cshtml", model);
这篇关于MVC局部视图无法渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!