Question

Ok so I am working with an ASP.NET MVC 3 project. When I first started to learn C# ASP.NET, I created a strongly typed view for a controller action. However, I now realized that I don't need that model (model that my view was strongly typed to) anymore. How can I get rid of the model? I tried excluding it from the project but when I compile my project in VS2010, it says that The page not found. Is there any way to undo the strongly typed part in VS2010 and still be able to keep and display stuff in my view?

Edit:

@using IntraNet.Areas.Roc.Models.ViewModels; @* This is for SiteLookupVM *@
@{ Layout = "~/Views/Shared/_LayoutIntranet.cshtml"; }

<div id="mdPageHdr">FindnSave Metrics</div>
@Html.Partial("_PartialSiteLookup", (SiteLookupVm) ViewBag.SiteLookup)




@if (ViewBag.SiteId > 0)
{   @Html.ActionLink("Bounce Metrics", "BounceMetrics", "Metrics",new { siteId =     @ViewBag.SiteId }, null)
@Html.Partial("_PartialChartDisplay")
}
else
{ 
<div id="tabsWrapper">
<ul id="tabs_menu">
<li>@Html.ActionLink("Page Loads", "Index", null, new { id = "active" })</li>
<li>@Html.ActionLink("Metrics Data", "DashMetrics")</li>
</ul>
</div>

<div id='dashboard'>

    @Html.Partial("_partialMetricsDashboard")
    </div>

 }

Action

protected MetricsDashboardRepository _mdr = new MetricsDashboardRepository();
private LoadHistoryDBEntities db = new LoadHistoryDBEntities();
private AvailabilityContext context = new AvailabilityContext();

public ActionResult Index(int? SiteId)
    {
        SiteLookupVm SiteLookup = new SiteLookupVm();
        SiteLookup.ActionName = "Index";
        SiteLookup.ControllerName = "Metrics";
        ViewBag.SiteLookup = SiteLookup;
        ViewBag.Title = "FNS Metrics";

        if (SiteId != null && SiteId > 0)
        {
            string site = new WPDE().Sites.Where(r => r.SiteId == SiteId).Select(r => r.Name).FirstOrDefault();
            SiteLookup.CurrentSite = site == null ? "Site does not exist" : site;
            ViewBag.SiteId = SiteId;
            ViewBag.FnsId = db.fns_sites.Where(f => f.3id == SiteId && f.enabled).Select(s => s.id).FirstOrDefault();
            ViewBag.RegionData = context.Regions.ToList();
            ViewBag.PathData = context.TestPaths.ToList();
        }
        else
        {
            _mdr.DashSetup();
            ViewBag.mdr = _mdr;
            ViewBag.date = 30;
            ViewBag.order = 0;
            ViewBag.totPageView = _mdr.TotPageView();
            ViewBag.totMonthUni = _mdr.TotMonthUni();
        }
        return View();
    }
Était-ce utile?

La solution

In the top of your view, you should see something like this:

Razor:

@model YourType

ASPX:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<YourType>" %>

If it's Razor, remove that line. If it's ASPX, change <YourType> to <dynamic>

Regards

Autres conseils

Following on the heels of Andre's answer below. I would suggest changing the ViewModel from 'YourType' to 'dynamic'.

That is was MVC does when you opt to not use a strongly-typed View.

In ASP.NET:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

and in Razor, just remove the '@model' line from top of your page.

For more information on .NET 4.0 Dynamic Types check out this: Dynamic .NET (MSDN Mag)

You say that when you compile it you get a 'the page not found' error. This sounds like a browser error message to me, which you would get when running your project and browsing to the page - not just compiling it.

Are you seeing this 'page not found' error in the browser? If so, check the URL in the browser, this is probably an invalid URL.

If it is something like http://localhost:12234/View/Blah/Blah.cshtml, try removing the /View/Blah/Blah.cshtml bit and see what happens.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top