MVC 5 - The type arguments for method "..." cannot be inferred from the usage. Try specifying the type arguments explicitly

asp.net-mvc asp.net-mvc-5 azure c# entity-framework

Question

I've spent the last few weeks learning MVC 5. I had hardly any problems up until yesterday. Everything was running smoothly up until last night when I opened Visual Studio to work on some of my projects.

My problems

I've restarted my computer, fixed my VS installation, deleted temporary directories from AppData, and other things to get this out of the way. I've looked at every option I can think of. Next, my problems.

I started by quitting my existing project and starting a new one with the identical database model. An Azure SQL database serves as the database. I made my own class to add data annotations that look like this for each of the objects: ( There were a lot of data annotations, so I deleted them all. The mistake is still present)

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

And here is the object EF that my database generated:

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

Then, utilizing my freshly formed model and the context that EF created rather than ApplicationDBContext, I created a scaffolded item.

The ViewBag.Title = now appears when I access the created views ""One or more types required to construct a dynamic expression cannot be found," is the error I get when I try to use the title. Do you need a reference?" Note that both the index and the CRUD cshtml files experience this.

Furthermore, on each and every @Html.LabelFor / @Html.DisplayNameFor as well as anything that ends in "I get the following error message when I try to use "For": "The type arguments for method 'System.Web.Mvc.Html.DisplayNameExtensions.DisplayNameFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>)' cannot be inferred from the usage. Try explicitly defining the type arguments."

Here is an example of a view. Since everything was generated automatically, I made no changes.

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

and its manager:

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

With that said, I also want to note out that none of this occurs when I run Visual Studio as administrator, which I don't understand. Everything also runs and builds normally.

I'm sorry for the long article, but I didn't want to omit anything. I also sorry if I used any phrases incorrectly because I'm still learning about MVC and C# in general.

Please let me know if there is anything else I can include.

1
5
10/13/2014 6:05:34 PM

Accepted Answer

ZZZ_tmp
1
8/10/2015 10:33:31 AM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow