我可以在.aspx页中使用ViewBag吗? [英] Can I use ViewBag in an .aspx page?

查看:312
本文介绍了我可以在.aspx页中使用ViewBag吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将UI页面从.cshtml文件移动到.aspx文件.现在,我遇到了一些编译错误.

I have to move my UI page from a .cshtml file to an .aspx file. Now I'm having a couple of compiling errors.

首先是'ViewBag'在当前上下文中不存在.我不能在.aspx中使用它吗?如果没有,什么是好的替代品?

First is that 'ViewBag' does not exist in the current context. Can I not use it in .aspx? If not, what is a good substitute?

第二,.cshtml页面具有模型声明:

Second, the .cshtml page had a model declaration:

@model myProject.Models.Navigation

我对其进行了更改,以便它可以在.aspx页中按如下方式工作:

I changed it so that it would work in the .aspx page as follows:

<%@ Import Namespace="myProject.Models" %>

我仍然不确定这是否是正确的替代方法,因为在没有出现错误的情况下,我无法包含"Navigation"一词.现在,在我曾经拥有的代码中:

I'm still not sure that's a correct substitute, because I could not include the word "Navigation" without getting an error. And now, in the code where I used to have:

@foreach (myProject.Models.Navigationitem item in Model.navigationItems){...

我已将其替换为:

<% foreach (myProject.Models.Navigationitem item in Model.navigationItems){...

我收到此错误:

名称"Model"在当前上下文中不存在

The name 'Model' does not exist in the current context

显然,我是唯一一个从剃须刀变成aspx的人,因为在线上确实有关于zilch的信息.感谢任何帮助.

Apparently, I'm the only guy who has ever gone from razor to aspx, because there's exactly zilch about it online. Appreciate any help.

推荐答案

WebForms通常不使用ViewBag,这只是使数据可用于ASP.Net MVC中的一种方法.使用WebForms,使数据可用于视图"(包含HTML的aspx页面)的一种好方法是公开包含该数据的属性.

WebForms don't usually use a ViewBag, which is just a way to make data available to your View in ASP.Net MVC. With WebForms, a nice way to make data available to your "View" (the aspx page containing the HTML) is to expose a property containing that data.

MVC的方法可能是在Controller中设置ViewBag.MyValue = "Some Value";,并在视图中使用<h1>@ViewBag.MyValue</h1>引用它.要在WebForms中执行等效操作,您首先需要在代码背后定义一个属性:

The MVC way might be to set ViewBag.MyValue = "Some Value"; in your Controller, and reference it in your view with <h1>@ViewBag.MyValue</h1>. To do the equivalent in WebForms you would first define a property in your codebehind:

protected string MyValue { get; set; }

然后,将值设置在某个地方,也许在您的Page_Load中:

Then, set the value somewhere, perhaps in your Page_Load:

protected void Page_Load (object sender, EventArgs e)
{
    this.MyValue = "Some Value";
}

并使用WebForms语法在页面上写值:

And write the value on the page using WebForms syntax:

<h1><%= MyValue %></h1>

对于您的特定情况,您似乎实际上并没有使用ViewBag.没关系,您还可以将对象用作属性:

For your specific case, you don't seem to actually be using ViewBag. That's ok, you can make objects available as properties also:

protected MyProject.Models.Navigation Model { get; set; }

protected void Page_Load (object sender, EventArgs e)
{
    this.Model = SomeMethodThatReturnsModel();
}

在定义了属性并设置了值之后,上面用于ASPX的代码应该可以正常工作.

With the property defined and the value set, the code you have above for your ASPX should work just fine.

这篇关于我可以在.aspx页中使用ViewBag吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆