简体   繁体   English

在MVC中创建ASPX视图的正确方法是什么?

[英]What is the PROPER way to create an ASPX View in MVC?

Essentially, my issue is this: I'm trying to display a ViewData variable on an ASPX page, but I'm getting "ViewData does not exist in the current context." 本质上,我的问题是这样的:我试图在ASPX页面上显示ViewData变量,但是却得到“当前上下文中不存在ViewData”。

What led me here? 是什么把我引到这里的? I'm new to MVC, and am following an online tutorial. 我是MVC的新手,并且正在关注在线教程。 In Lesson 1, he has us create a view by going to Add > View, which creates a Razor view. 在第1课中,他让我们通过转到添加>视图来创建视图,这将创建一个Razor视图。 Then, in the next tutorial, he's teaching us to use ViewData to pass a variable to the view. 然后,在下一个教程中,他教我们如何使用ViewData将变量传递给视图。 But, when he displays it, he's doing so using the <%: %> method, which doesn't work on my Razor page. 但是,当他显示它时,他是使用<%:%>方法执行的,该方法在我的Razor页面上不起作用。 So, I try to create an ASPX page in my View folder by creating a Web Form the traditional way. 因此,我尝试通过传统方式创建Web窗体在View文件夹中创建ASPX页面。 When I do that, I get the error mentioned above. 当我这样做时,我得到上面提到的错误。 I have successfully figured out how to get the Razor method working in my cshtml page, but what is the appropriate method should I want to do it the "angle bracket - percent" way? 我已经成功地弄清楚了如何在我的cshtml页面中使用Razor方法,但是我想以“尖括号-百分比”的方式来实现它的合适方法是什么?

Edit: Basically, all I did was follow the instructions for Tutorial 2 in the following walk-through. 编辑:基本上,我所做的只是在以下演练中按照教程2的说明进行操作。 He doesn't really explain HOW he created the ASPX page (or that he used an ASPX page at all). 他并没有真正解释他如何创建ASPX页面(或者根本不使用ASPX页面)。 If anyone knows of a better tutorial that's more complete, I'm all ears. 如果有人知道更好,更完整的教程,我将不知所措。 http://www.codeproject.com/Articles/207797/Learn-MVC-Model-View-Controller-step-by-step-in#Lab1:- Creating a simple hello world ASP.NET MVC Application http://www.codeproject.com/Articles/207797/Learn-MVC-Model-View-Controller-step-by-step-in#Lab1:-创建一个简单的Hello World ASP.NET MVC应用程序

Edit2: By request, here's my code: Edit2:应要求,这是我的代码:

ASPX: ASPX:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="MVCLearn.Views.FirstMVC.Index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <%= ViewData["vwTest"] %>
    </div>
    </form>
</body>
</html>

And here is my Controller: 这是我的控制器:

    public class FirstMVCController : Controller
    {
        // GET: FirstMVC
        public ActionResult Index()
        {
            ViewData["vwtest"] = "Something";
            return View();
        }

        public ActionResult SayHello()
        {
            return View("HelloView");
        }
    }
      <%= ViewData["vwTest"] %>
  ViewData["vwtest"] = "Something";

vwTest != vwtest vwTest!= vwtest

You should write same name 你应该写同样的名字

   <%= ViewData["vwTest"] %>
      ViewData["vwTest"] = "Something";

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

相关问题 使用部分视图MVC 4从父表单提交数据的正确方法是什么? - What is the proper way to submit data from Parent form with partial view MVC 4? 在WPF中创建UI设置的正确方法是什么? - What is the proper way to create UI settings in WPF? 什么是创建等效功能的正确方法 - What is a proper way to create awaitable function 用颜色创建画笔的正确方法是什么? - What is the proper way to create a brush from a color? 创建和处置系统计时器的正确方法是什么? - What is the proper way to create and dispose a system timer? 寻找使用ajax将MVC 5视图发布到控制器的正确方法 - Looking for proper way to use ajax to post an MVC 5 View to a controller 拆分大型mvc应用程序的解决方案的正确方法是什么? - What is the proper way to split a solution of a big mvc application? 在ASP.NET MVC中连接两个表的正确方法是什么? - What is the proper way to Join two tables in ASP.NET MVC? 在MVC5布局页面中缓存用户配置文件的正确方法是什么 - What is the proper way to cache a user profile in the MVC5 layout page ASP.NET MVC - 从视图中创建 Url 到 controller 动作的最佳方法是什么? - ASP.NET MVC - What's the best way to create a Url to controller action from within view?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM