MVC 5 - 方法的类型参数" ..."无法从使用中推断出来。尝试显式指定类型参数

时间:2014-10-13 17:59:03

标签: c# asp.net-mvc entity-framework azure asp.net-mvc-5

过去几周我一直在学习MVC 5。到目前为止,我几乎没有任何问题,直到昨天。一切都工作正常,直到昨晚我打开VS来处理我的项目。

我的问题:

为了解决一些问题,我已重新启动计算机,修复了我的VS安装,从AppData等中删除了临时文件夹。我已经用尽了所有可能找到的解决方案。现在谈谈我的问题。

首先,我停止了当前项目的工作,并使用相同的数据库模型创建了一个新项目。该数据库是Azure SQL数据库。对于每个对象,我创建了自己的类来应用如下所示的数据注释:(我删除了所有数据注释,因为有很多。错误仍然存​​在)

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;

namespace Dyad.GreenvilleFoodTrucks.Data
{
[MetadataType(typeof(LocationMetadata))]
public partial class Location
{
}

public class LocationMetadata
{
    public int ID { get; set; }
    public int TruckID { get; set; }
    public Nullable<System.DateTime> StartTime { get; set; }
    public Nullable<System.DateTime> EndTime { get; set; }
    public Nullable<System.DateTime> Date { get; set; }
    public string Description { get; set; }
    public string Address { get; set; }
    public string State { get; set; }
    public string ZIP { get; set; }

    public virtual Truck Truck { get; set; }
}
}

从我的数据库创建的对象EF:

namespace Dyad.GreenvilleFoodTrucks.Data
{
using System;
using System.Collections.Generic;

public partial class Location
{
    public int ID { get; set; }
    public int TruckID { get; set; }
    public Nullable<System.DateTime> StartTime { get; set; }
    public Nullable<System.DateTime> EndTime { get; set; }
    public Nullable<System.DateTime> Date { get; set; }
    public string Description { get; set; }
    public string Address { get; set; }
    public string State { get; set; }
    public string ZIP { get; set; }

    public virtual Truck Truck { get; set; }
}
}

然后我使用我新创建的模型制作了一个脚手架项目,上下文是EF创建的,而不是ApplicationDBContext。

现在,当我进入生成的视图时,ViewBag.Title =&#34;标题&#34 ;;给我这个错误: &#34;无法找到编译动态表达式的一种或多种类型。你错过了参考文献吗?&#34;请注意,这会发生在所有CRUD cshtml文件以及索引上。

除此之外,还有每一个@ Html.LabelFor / @ Html.DisplayNameFor以及以#34; For&#34;结尾的所有内容。给我这个错误: &#34;方法的类型参数&#39; System.Web.Mvc.Html.DisplayNameExtensions.DisplayNameFor(System.Web.Mvc.HtmlHelper,System.Linq.Expressions.Expression&gt;)&#39;无法从使用中推断出来。尝试明确指定类型参数。&#34;

这是一个视图的样子。这都是自动生成的,所以我没有改变任何东西。

@model Dyad.GreenvilleFoodTrucks.Data.Location

@{
ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm()) 
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Location</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.TruckID, "TruckID", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("TruckID", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.TruckID, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.StartTime, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.StartTime, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.StartTime, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.EndTime, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.EndTime, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.EndTime, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Date, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.State, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.State, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.State, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.ZIP, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.ZIP, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.ZIP, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

及其控制者:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Dyad.GreenvilleFoodTrucks.Data;

namespace Dyad.GreenvilleFoodTrucks.Controllers
{
public class LocationsController : Controller
{
    private GreenvilleFoodTrucksEntities db = new GreenvilleFoodTrucksEntities();

    // GET: Locations
    public ActionResult Index()
    {
        var locations = db.Locations.Include(l => l.Truck);
        return View(locations.ToList());
    }

    // GET: Locations/Details/5
    public ActionResult Details(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Location location = db.Locations.Find(id);
        if (location == null)
        {
            return HttpNotFound();
        }
        return View(location);
    }

    // GET: Locations/Create
    public ActionResult Create()
    {
        ViewBag.TruckID = new SelectList(db.Trucks, "ID", "Name");
        return View();
    }

    // POST: Locations/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "ID,TruckID,StartTime,EndTime,Date,Description,Address,State,ZIP")] Location location)
    {
        if (ModelState.IsValid)
        {
            db.Locations.Add(location);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.TruckID = new SelectList(db.Trucks, "ID", "Name", location.TruckID);
        return View(location);
    }

    // GET: Locations/Edit/5
    public ActionResult Edit(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Location location = db.Locations.Find(id);
        if (location == null)
        {
            return HttpNotFound();
        }
        ViewBag.TruckID = new SelectList(db.Trucks, "ID", "Name", location.TruckID);
        return View(location);
    }

    // POST: Locations/Edit/5
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "ID,TruckID,StartTime,EndTime,Date,Description,Address,State,ZIP")] Location location)
    {
        if (ModelState.IsValid)
        {
            db.Entry(location).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        ViewBag.TruckID = new SelectList(db.Trucks, "ID", "Name", location.TruckID);
        return View(location);
    }

    // GET: Locations/Delete/5
    public ActionResult Delete(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Location location = db.Locations.Find(id);
        if (location == null)
        {
            return HttpNotFound();
        }
        return View(location);
    }

    // POST: Locations/Delete/5
    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteConfirmed(int id)
    {
        Location location = db.Locations.Find(id);
        db.Locations.Remove(location);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }
}
}

解决了所有这些问题后,我还要指出,当我以管理员身份运行VS时,这一切都不会发生,我不明白。此外,每个编译和运行照常。

我为冗长的写作道歉,但我不想遗漏任何东西。如果我滥用任何条款,我也会道歉,因为我对MVC和C#一般都是新手。

如果我还有其他任何内容,请告诉我。

2 个答案:

答案 0 :(得分:3)

当视图文件夹中的web.config文件引用旧版或缺少版本的MVC或Razor依赖项时,有时会发生这种情况。如果您无法手动修复它,或找出哪个引用有问题,最简单的方法是创建一个新的MVC项目,并将View文件夹中的web.config文件与现有项目进行比较。配置文件。

虽然它在运行或调试时通常不会破坏任何东西,但它确实会让Intellisense感到困惑。

答案 1 :(得分:1)

我在视图文件夹的web.config中更改剃刀视图的基本类型时遇到了这种情况。

我正确地改变了这一部分

  <system.web.webPages.razor>
    <pages pageBaseType="My.CustomType">
  </system.web.webPages.razor>

然后我错误地更改了<system.web>部分以包含我的自定义类型。我不得不将其更改为完全限定的System.Web.Mvc.ViewPage,类似于

<system.web>
    <pages
        validateRequest="false"
        pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
    </pages>
</system.web>

清理并重建项目后,所有类型参数错误都消失了,我仍在使用我的自定义类型进行<system.web.webPages.razor>

中定义的剃刀视图