简体   繁体   English

如何撤消强类型视图?

[英]How to undo strongly typed view?

Ok so I am working with an ASP.NET MVC 3 project. 好的,所以我正在使用ASP.NET MVC 3项目。 When I first started to learn C# ASP.NET, I created a strongly typed view for a controller action. 刚开始学习C#ASP.NET时,我为控制器操作创建了一个强类型的视图。 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 . 我尝试将其从项目中排除,但是当我在VS2010中编译我的项目时,它说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? 有什么方法可以撤消VS2010中的强类型部分,并且仍然能够保留并显示我所看到的内容?

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

In the top of your view, you should see something like this: 在视图的顶部,您应该看到类似以下内容:

Razor: 剃刀:

@model YourType

ASPX: 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> 如果是ASPX, <YourType>更改为<dynamic>

Regards 问候

Following on the heels of Andre's answer below. 紧随安德烈(Andre)的回答。 I would suggest changing the ViewModel from 'YourType' to 'dynamic'. 我建议将ViewModel从“ YourType”更改为“ dynamic”。

That is was MVC does when you opt to not use a strongly-typed View. 这就是您选择不使用强类型视图时MVC所做的事情。

In ASP.NET: 在ASP.NET中:

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

and in Razor, just remove the '@model' line from top of your page. 在Razor中,只需从页面顶部删除“ @model”行。

For more information on .NET 4.0 Dynamic Types check out this: Dynamic .NET (MSDN Mag) 有关.NET 4.0动态类型的更多信息,请查看以下内容: 动态.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. 如果是这样,请在浏览器中检查URL,这可能是无效的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. 如果类似于http://localhost:12234/View/Blah/Blah.cshtml ,请尝试删除/View/Blah/Blah.cshtml位,然后看看会发生什么。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM