[英]ViewBag RuntimeBinderException (MVC 4, ASPX)
I started up a Default MVC 4 project using the ASPX
engine (not Razor), and everything works fine. 我使用
ASPX
引擎(不是Razor)启动了一个Default MVC 4项目,一切正常。
I can use 我可以用
<% Model.Name %>
and 和
<% Model.Rating %>
for book ratings (instead of the Restaurant Review that is up on the asp.net/mvc tutorials site). 用于收视率(而不是asp.net/mvc教程网站上的餐厅评论)。 However, my code errors on the
<% ViewBag.Message %>
但是,我的代码错误在
<% ViewBag.Message %>
...so for some reason, when inheriting from the "BookReviewer" Class within the BookReview model, I can view the Model information but not the Viewbag
? ...所以出于某种原因,当继承BookReview模型中的“BookReviewer”类时,我可以查看模型信息而不是
Viewbag
? Am I missing something? 我错过了什么吗? Code below.
代码如下。
[Controller] HomeController.cs: [Controller] HomeController.cs:
public class HomeController : Controller
{
var model = new BookReview()
{
Name = "Pro jQuery For Noobs",
Rating = 7
};
return View(model);
}
[Model] BookReview.cs [型号] BookReview.cs
namespace Bookreview.Models
{
public class BookReview
{
public string Name { get; set; }
public int Rating { get; set; }
}
}
[View] Index.aspx: [查看] Index.aspx:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<BookRevew.Models.BookReviewer>" %>
<asp:Content ID="indexFeatured" ContentPlaceHolderID="FeaturedContent" runat="server">
<h2>
<% ViewBag.Message %>
<% Model.Name %>
<% Model.Rating %>
</h2>
</asp:content>
As we can see by your comments, you are setting the ViewBag.Message
in another controller. 正如我们在您的评论中看到的那样,您正在另一个控制器中设置
ViewBag.Message
。 The ViewBag
is only for data for the view that was set by the controller that served the view . ViewBag
仅用于由提供视图的控制器设置的视图的数据 。
If you need to send it from another controller, you should use a parameter in your action method, then you can set the ViewBag.Message
to the passed in parameter. 如果需要从另一个控制器发送它,则应在操作方法中使用参数,然后可以将
ViewBag.Message
设置为传入的参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.