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

标签 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。

现在,当我进入生成的 View 时, ViewBag.Title = "title";给我这个错误: “找不到编译动态表达式所需的一个或多个类型。您是否缺少引用?”请注意,这种情况发生在所有 CRUD cshtml 文件以及索引上。

除此之外,在每个 @Html.LabelFor/@Html.DisplayNameFor 以及任何以“For”结尾的内容上都会出现此错误: “无法从用法中推断出方法“System.Web.Mvc.Html.DisplayNameExtensions.DisplayNameFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>)”的类型参数。尝试显式指定类型参数”

这是 View 的样子。这都是自动生成的,所以我没有更改任何内容。

@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")
}

及其 Controller :

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# 总体来说还很陌生。

如果我还可以添加任何其他内容,请告诉我。

最佳答案

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

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

关于c# - MVC 5 - 无法从用法推断方法 "..."的类型参数。尝试显式指定类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26346053/

相关文章:

c# - Protobuf-net 在与泛型一起用于反序列化时要求 TypeModel.CS

asp.net-mvc-3 - 具有最佳实践的示例N层ASP.NET MVC3应用程序(使用EF 4.1)

linq - 数据访问应用程序 block 、NHibernate、ADO.NET Entity Framework 和 LINQ to SQL 之间的主要区别是什么?

javascript - 可以在 MVC 的 Controller 中访问隐藏变量值吗

c# - 将企业库 5 迁移到 6

javascript - Angular 2 - TypeScript 文件未解析为 .js 扩展名 .NET MVC 应用程序

c# - 获取 Entity Framework 连接字符串

c# - 根据窗口大小自动缩放按钮 C#/XAML

c# - 是否应该在某个地方处理所有抛出的异常?

c# - C# 中的 UTC 时间戳(以毫秒为单位)到短日期时间字符串