简体   繁体   中英

'The type or namespace name 'BookModel' could not be found ASP.NET MVC

So I am trying to display a Model based on an XML file in my View but I get the error as the title says. I have tried adding all references there are and it still does not work.

And also a side question, I am trying to make it able to search for any book (datatype) and the results should be displayed, shall I then give the LINQ statement to a variable or what is the best solution?

Here's my code:

Model:

using System;

namespace Andre_Kordasti___Programmeringsuppgift.Models
{
    public class BookModel
    {
        public string Author { get; set; }
        public string Title { get; set; }
        public string Genre { get; set; }
        public decimal Price { get; set; }
        public DateTime PublishDate { get; set; }
        public string Description { get; set; }
    }
}

View:

@model IEnumerable<BookModel> @*error*@

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Sök</h2>

<input type="text" />
<input type="button" value="Sök" />
<br />
<a href="~/DataSource/books.xml">~/DataSource/books.xml</a>

@foreach (var book in Model)
{
    @Html.DisplayFor(m => book.Author)
    @Html.DisplayFor(m => book.Title)
}

Controller:

using System;
using System.Linq;
using System.Web.Mvc;
using System.Xml.Linq;
using Andre_Kordasti___Programmeringsuppgift.Models;

namespace Andre_Kordasti___Programmeringsuppgift.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            const string FILENAME = @"c:\users\andre\documents\visual studio 2015\Projects\Andre Kordasti - Programmeringsuppgift\Andre Kordasti - Programmeringsuppgift\DataSource\books.xml";

            XDocument doc = XDocument.Load(FILENAME);

            var results = doc.Descendants("book").Select(x => new
            {
                id = (string)x.Attribute("id"),
                author = (string)x.Element("author"),
                genre = (string)x.Element("genre"),
                price = (decimal)x.Element("price"),
                date = (DateTime)x.Element("publish_date"),
                description = (string)x.Element("description")
            }).ToList();

            var model = new BookModel();

            return View(results);
        }

        public ActionResult Fibonacci()
        {
            return View();
        }
    }
}

XML File:

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
</catalog>

There seems to be a couple of issues in your code.

Firstly, try to use the fully qualified name instead of partial name in your View :

@model IEnumerable<Andre_Kordasti___Programmeringsuppgift.Models.BookModel>

If you use partial name, you have to first use using namespace_name keyword before writing it.

@using Andre_Kordasti___Programmeringsuppgift.Models;
@model IEnumerable<BookModel>

Secondly, notice that the item you are passing from your Controller is anonymous, not exactly the BookModel :

var results = doc.Descendants("book").Select(x => new //anonymous here
{
    id = (string)x.Attribute("id"),
    author = (string)x.Element("author"),
    genre = (string)x.Element("genre"),
    price = (decimal)x.Element("price"),
    date = (DateTime)x.Element("publish_date"),
    description = (string)x.Element("description")
}).ToList();

Change to:

var results = doc.Descendants("book").Select(x => new BookModel() //note the syntax here
{
    //Id = (string)x.Attribute("id"),
    Author = (string)x.Element("author"),
    Genre = (string)x.Element("genre"),
    Price = (decimal)x.Element("price"),
    PublishDate = (DateTime)x.Element("publish_date"),
    Description = (string)x.Element("description")
    //use the same exact properties as your BookModel
}).ToList(); //ToList() may not be needed actually

var model = new BookModel(); //And this is...?

you should use

using Andre_Kordasti___Programmeringsuppgift.Models;

before mentioning BookModel

@model IEnumerable<BookModel> 

Edit: Change your Action method to the following

const string FILENAME = @"c:\users\andre\documents\visual studio 2015\Projects\Andre Kordasti - Programmeringsuppgift\Andre Kordasti - Programmeringsuppgift\DataSource\books.xml";

        XDocument doc = XDocument.Load(FILENAME);

        return View(doc.Descendants("book").Select(x => new BookModel
        {
            Title = (string)x.Element("title"),
            Author = (string)x.Element("author"),
            Genre = (string)x.Element("genre"),
            Price = (decimal)x.Element("price"),
            Date = (DateTime)x.Element("publish_date"),
            Description = (string)x.Element("description")
        }));

The error you mentioned can be caused by the fact you are missing using statement in your view, or alternatively passing the model type with full namespace. But even if you add this, it would not work, as you do not pass the collection you are suppose to pass - in your controller you pass the result , which is a collection of anonymous objects, not BookModel :

var results = doc.Descendants("book").Select(x => new BookModel // <= missing
    {
        Author = x.Element("author").Value,
        Price = decimal.Parse(x.Element("price").Value)
        /* your properties*/
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM