Can I use ViewBag in an .aspx page?
Asked Answered
T

3

7

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

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?

Second, the .cshtml page had a model declaration:

@model myProject.Models.Navigation

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

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

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){...

I've replaced it with:

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

And I get this error:

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

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.

Tartaric answered 1/3, 2016 at 0:17 Comment(4)
Can I ask you why you're going from Razor to ASPX? People are moving the opposite direction for a reason. And did you also move from MVC to Web Forms, or are you still doing MVC?Mezzo
@Mezzo you are right,but still if he want's to move he has to do with the Dynamic ViewState :Please find the article ,hope it will solve your problem. codeproject.com/Articles/316326/Dynamic-ViewState-in-ASP- Net-WebFormsVorticella
@Vivek let's not assume Web Forms. ASPX can be used with MVC.Mezzo
@Mezzo Valid question. I'm working with a legacy code base that uses asp.net classic and web forms. I thought I'd be able to sneak in an MVC page, but I'm not knowledgable enough yet in asp.net to know how to make that work, and it caused many server errors. The solution was to eliminate MVC altogether and rewrite the http requests using web forms. I anxiously look forward to moving on to a project that uses modern tech!Tartaric
P
8

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.

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; }

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

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

And write the value on the page using WebForms syntax:

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

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();
}

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

Perilune answered 2/3, 2016 at 21:11 Comment(0)
C
1

Your page should have

<%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage(Of myProject.Models.Navigation)" %>

at the top to specify the model type.

Checkroom answered 1/3, 2016 at 22:8 Comment(0)
S
0

Instead of ViewBag, you can use ViewState, as shown in the example below.

private List<TrimPackage> Packages
{
    get
    {
        return (List<TrimPackage>)ViewState["Packages"];
    }

    set
    {
        ViewState["Packages"] = value;
    }
}
Susysuter answered 9/8, 2021 at 16:5 Comment(1)
but you forgot to explain what the difference between ViewState and ViewBagCollectionIntreat

© 2022 - 2024 — McMap. All rights reserved.