0
は私のMVCアプリケーションにXMLファイルをロードしているが、関係なく、私はそれを実装する方法の、それは常にゼロの数を返し、私はイムは正確に最善をイマイチやって道を知っていますmigrationsメソッドで作成された構成クラスにxmlローダーを格納することで、動作していましたが、引用符は停止しました。「アドレスの一部にしか到達できません」これを修正する方法があるのだろうか?Asp.net MVCのXML私がやろうとしています何
Catalog.cs
public class CatalogController : BaseController
{
// GET: Catalog
public ActionResult Index()
{
List<Category> Categories = DbContext.Categories.OrderBy(c => c.Name).ToList();
int NumberofBooks = DbContext.Books.Count();
List<PublicationThumbnail> bookItems = GetBookThumbNails(5);
HomeViewModel homeViewModel = new HomeViewModel(Categories, NumberofBooks, bookItems);
return View(homeViewModel);
}
public ActionResult Search(string c = null, string t = null)
{
string searchMessage = (t == null ? "Publications" : t);
if (c != null)
{
searchMessage += " in " + c;
}
List<PublicationThumbnail> imageThumbNail =
FilterPublications(c, t).Select(p => new PublicationThumbnail(p)).ToList();
SearchViewModel searchViewModel = new SearchViewModel()
{
FilterMessage = searchMessage,
Thumbnails = imageThumbNail,
ResultsCount = imageThumbNail.Count
};
return View(searchViewModel);
}
public ActionResult View(int id)
{
Publication foundPublications = DbContext.Publications.Find(id);
if (foundPublications != null)
{
Account currentUser = null;
if (User.Identity.IsAuthenticated)
{
currentUser = AccountManager.GetCurrentUser(User);
}
return View(new PublicationViewModel(foundPublications, currentUser));
}
return HttpNotFound();
}
[Authorize]
public ActionResult Download(int id)
{
Account account = AccountManager.GetCurrentUser(User);
if (account != null && (account.Subscribed() || account.PurchasedItem(id)))
{
Publication publication = DbContext.Publications.FirstOrDefault(p => p.Id == id);
return View(publication);
}
return HttpNotFound();
}
private List<Publication> FilterPublications(string category, string publicationType)
{
List<Publication> results = new List<Publication>();
if (publicationType != null)
{
if (publicationType.ToLower() == "books")
{
results.AddRange(DbContext.Books);
}
}
else
{
results.AddRange(DbContext.Publications);
}
if (category != null)
{
Category categoryMatch = DbContext.Categories.FirstOrDefault(c => c.Name == category);
if (categoryMatch != null)
{
results = results.Where(p => p.Categories.Contains(categoryMatch)).ToList();
}
}
return results;
}
private List<PublicationThumbnail> GetBookThumbNails(int count)
{
List<Book> books = DbContext.Books.Take(Math.Min(count, DbContext.Books.Count())).ToList();
return books.Select(j => new PublicationThumbnail(j)).ToList();
}
}
のBooks.xml
<?xml version="1.0" encoding="utf-8" ?>
<Books>
<Book>
<Title>The Rabbit Who Wants to Fall Asleep: A New Way of Getting Children to Sleep</Title>
<Description>The groundbreaking No. 1 bestseller is sure to turn nightly bedtime battles into a loving and special end-of-day ritual. This child-tested, parent-approved story uses an innovative technique that brings a calm end to any child's day.</Description>
<Authors>Carl-Johan Forssén Ehrlin</Authors>
<Chapters>4</Chapters>
<Edition>1</Edition>
<PublicationDate>2016-05-21T10:50:23.5602265-04:00</PublicationDate>
<PublicationFormat>PDF</PublicationFormat>
<FileLocation>App_Data/Publications</FileLocation>
<ISBN>978-3-319-30715-2</ISBN>
<Categories>
<Category>
<Name>Other</Name>
</Category>
</Categories>
<ThumbnailLocation>TheRabbit.jpg</ThumbnailLocation>
<Price>4.00</Price>
</Book>
</Books>
Configurationクラス
internal sealed class Configuration : DbMigrationsConfiguration<HullUniversityPress.DataAccessLayer.HullPressContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(HullPressContext context)
{
if (context.Books.Count() == 0)
{
SeedPublicationCategories(context);
SeedBooks(context);
SeedAccounts(context);
SeedSubscriptions(context);
}
}
private XDocument LoadXmlDoc(string fileName)
{
XDocument xmlDoc = XDocument.Load("HullUniversityPress/App_Data/" + fileName);
return xmlDoc;
}
private void SeedBooks(HullPressContext context)
{
XDocument xmlDoc = LoadXmlDoc("Books.xml");
foreach (XElement j in xmlDoc.Root.Elements("Book"))
{
Book newBook = new Book()
{
Title = j.Element("Title").Value,
Description = j.Element("Description").Value,
ISBN = j.Element("ISBN").Value,
FileLocation = j.Element("FileLocation").Value,
ThumbnailLocation = j.Element("ThumbnailLocation").Value,
Chapters = int.Parse(j.Element("Chapters").Value),
Edition = int.Parse(j.Element("Edition").Value),
Langauge = (Language)Enum.Parse(typeof(Language), j.Element("Language").Value),
Authors = j.Element("Authors").Value,
Price = decimal.Parse(j.Element("Price").Value),
PublicationFormat = (PublicationFormat)Enum.Parse(typeof(PublicationFormat), j.Element("PublicationFormat").Value),
PublicationDate = DateTime.Parse(j.Element("PublicationDate").Value)
};
ParseCategories(newBook, context, j.Element("Categories"));
context.Books.Add(newBook);
}
}
private void ParseCategories(Publication publication, HullPressContext context, XElement categoryRoot)
{
publication.Categories = new List<Category>();
foreach (XElement category in categoryRoot.Elements("Category"))
{
string categoryName = category.Element("Name").Value;
Category match = context.Categories.Local.FirstOrDefault(c => c.Name == categoryName);
if (match != null)
{
publication.Categories.Add(match);
match.Publications.Add(publication);
}
else throw new Exception("Unidentified category: " + category.Element("Name").Value + ", Categories: " + context.Categories.Local.Count());
}
}
private void SeedPublicationCategories(HullPressContext context)
{
context.Categories.AddOrUpdate(
new Category("Other")
);
}
private void SeedAccounts(HullPressContext context)
{
context.Accounts.AddOrUpdate(
new Account("[email protected]", "Hello12345")
);
}
private void SeedSubscriptions(HullPressContext context)
{
Account account = context.Accounts.Local.FirstOrDefault(u => u.Email == "[email protected]");
Book bookSub = context.Books.Local.FirstOrDefault();
if (account != null && bookSub != null)
{
context.Purchases.Add(new Purchase()
{
Account = account,
Publication = bookSub,
Format = PublicationFormat.PDF
});
}
}
}
ヘルプの任意の並べ替え/右直接的に指摘を大幅に受信されるだろう。
:
XDocument
はBooks.xmlをの内容がロードされていますODEは、あなたの質問とは何の関係もありません - [どのように、最小限の完全な、かつ検証例を作成する](http://stackoverflow.com/help/mcve) –あなたは、コンテキストにアイテムを追加していますが呼び出していないようです。変更内容を保存() – Pawel