0

I have a problem, I am a beginner with MVC and ASP.NET, so I am trying to do some exercises from a book, but I don't find an error. I create a class, its name is Photo, and I added some Data Annotations, for example [DisplayName(Created Data)], and now I have this class:

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

namespace WebApplication2.Models
{
    public class Photo
    {
        public int PhotoID { get; set; }
        [DisplayName("Titolo")]
        [Required]
        public string Title { get; set; }
        public byte[] PhotoFile { get; set; }
        public string Description { get; set; }
        // [DisplayName("Data creazione")]
        // [DisplayFormat(DataFormatString = "{0:dd/mm/yy}", ApplyFormatInEditMode = true)]
        public DateTime CreateDate { get; set;  }
        // [DisplayName("Possessore")]
        public string Owner { get; set; }
        public virtual ICollection<Comment> Comments { get; set; }
    }
}

my view is:

<div id="photo-title">
    @Html.DisplayNameFor(model => model.Title)
    @Model.Title
</div>
<div>
    @Model.PhotoFile
</div>
<div>
    @Model.CreateDate
</div>

I get the error for model.Title, it is

an expression tree may not contain a dynamic operation.

What does it mean? What do I have to do to solve my problem, seeing the Name for Title to use the Annotations in the View?

ekad
  • 14,436
  • 26
  • 44
  • 46
Marchin
  • 23
  • 1
  • 6
  • 3
    How have you declared the model in the view - it should be `@model Photo` –  Aug 19 '16 at 12:15

2 Answers2

0

Your view is untyped, so your view is defaulting to dynamic. You have to make your view/model strongly typed.

You can achieve this by simply adding:

@model WebApplication2.Models.Photo // at the top of your view.

Ensure that it is indeed @model Photo and not @Model Photo as this mistake is common.

Grizzly
  • 5,873
  • 8
  • 56
  • 109
  • Yes, i did it after your answers, i don't get the error before any more, but i get an error about Photo, "it says the type or the namespace Photo has net been found. Probably i miss a using or a reference to an assembly. Where have i to add it? The class Photo is in the namespace: WebApplication2.Models. – Marchin Aug 19 '16 at 12:24
  • @Marchin, Just use the fully qualified name - `@model WebApplication2.Models.Photo` –  Aug 19 '16 at 12:27
  • using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace WebApplication2.Controllers { public class HomeController : Controller { // GET: Home public ActionResult Index() { Models.Photo newPhoto = new Models.Photo(); newPhoto.Title = "This is an example Photo"; newPhoto.Owner = User.Identity.Name; newPhoto.CreateDate = DateTime.Today; return View("Index", newPhoto); } } } – Marchin Aug 19 '16 at 12:27
  • @Marchin I have edited my answer – Grizzly Aug 19 '16 at 12:27
  • @Marchin Sorry I didn't need your controller, you just need to add your `Assembly Name.Models.Photo` so your View knows exactly where to get the information needed to display – Grizzly Aug 19 '16 at 12:29
  • @Marchin make to sure make an answer as accepted by clicking on the check mark next to the answer that you used to help resolve your issue, so that this question will show as having an answer, – Grizzly Aug 19 '16 at 12:33
0
@model WebApplication2.Models.Photo // model as strongly typed.


<div id="photo-title"> 
@model.Title
</div> 
Rajkamal C
  • 59
  • 2