I have been studying MVC 5 for the last couple of weeks. Until now, I have had few questions until yesterday. Everything worked fine until last night when I opened VS to work on my projects.
My problems:
To clean up some things, I restarted my computer, repaired the VS installation, deleted temporary folders from AppData, etc. I have exhausted all possible solutions that I could find. Now about my problems.
First of all, I stopped working on my current project and created a new project using the same database model. A database is an Azure SQL database. For each of the objects, I created my own class for applying data annotations that look like this: (I deleted all data annotations because there were a lot of them. The error still persists)
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 the EF object created from my database:
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 I made the lining element using my newly created model, with the context that was created by EF, not ApplicationDBContext.
Now, when I go to the generated views, ViewBag.title = "title"; gives me this error: "One or more types cannot be found to compile a dynamic expression. Did you not provide a link?" Note that this happens in all CRUD cshtml files, as well as in the index.
, @Html.LabelFor/@Html.DisplayNameFor, , "For", :
" " System.Web.Mvc.Html.DisplayNameExtensions.DisplayNameFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression > ) " . ."
. , .
@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();
public ActionResult Index()
{
var locations = db.Locations.Include(l => l.Truck);
return View(locations.ToList());
}
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);
}
public ActionResult Create()
{
ViewBag.TruckID = new SelectList(db.Trucks, "ID", "Name");
return View();
}
[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);
}
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);
}
[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);
}
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);
}
[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 admin, . , .
, . , , MVC # .
- , , , .