无法将项目添加到数据库中,modelState.isvalid == false



我正在使用ASP.NET MVC应用程序。基本上,我正在尝试执行以下操作:我创建了一个API助手类,该类别使从Google Books API返回的JSON数据进行了挑选。在我的create.cshtml中,我只想传递我要添加的书的ISBN,但是,正如我在调试器中发现的那样,ModelState.IS有效是错误的,因此新书没有创建。据我在调试器中看到的,所有数据都可以正确地从API中提取到字典中,但是由于某些原因,我无法将其存储DB。我知道可能有一个更优雅的解决方案,但是任何建议都非常欢迎。谢谢你的宝贵时间。

这是可能有帮助的代码文件:

apihelper:应对json数据并将其存储在字典中。

    namespace APIHelper
{
    public class IndustryIdentifier
    {
        [JsonProperty("type")]
        public string Type { get; set; }
        [JsonProperty("identifier")]
        public string Identifier { get; set; }
    }
    public class ReadingModes
    {
        [JsonProperty("text")]
        public bool Text { get; set; }
        [JsonProperty("image")]
        public bool Image { get; set; }
    }
    public class ImageLinks
    {
        [JsonProperty("smallThumbnail")]
        public string SmallThumbnail { get; set; }
        [JsonProperty("thumbnail")]
        public string Thumbnail { get; set; }
    }
    public class VolumeInfo
    {
        [JsonProperty("title")]
        public string Title { get; set; }
        [JsonProperty("subtitle")]
        public string Subtitle { get; set; }
        [JsonProperty("authors")]
        public IList<string> Authors { get; set; }
        [JsonProperty("publisher")]
        public string Publisher { get; set; }
        [JsonProperty("publishedDate")]
        public string PublishedDate { get; set; }
        [JsonProperty("description")]
        public string Description { get; set; }
        [JsonProperty("industryIdentifiers")]
        public IList<IndustryIdentifier> IndustryIdentifiers { get; set; }
        [JsonProperty("readingModes")]
        public ReadingModes ReadingModes { get; set; }
        [JsonProperty("pageCount")]
        public int PageCount { get; set; }
        [JsonProperty("printType")]
        public string PrintType { get; set; }
        [JsonProperty("categories")]
        public IList<string> Categories { get; set; }
        [JsonProperty("maturityRating")]
        public string MaturityRating { get; set; }
        [JsonProperty("allowAnonLogging")]
        public bool AllowAnonLogging { get; set; }
        [JsonProperty("contentVersion")]
        public string ContentVersion { get; set; }
        [JsonProperty("imageLinks")]
        public ImageLinks ImageLinks { get; set; }
        [JsonProperty("language")]
        public string Language { get; set; }
        [JsonProperty("previewLink")]
        public string PreviewLink { get; set; }
        [JsonProperty("infoLink")]
        public string InfoLink { get; set; }
        [JsonProperty("canonicalVolumeLink")]
        public string CanonicalVolumeLink { get; set; }
    }
    public class SaleInfo
    {
        [JsonProperty("country")]
        public string Country { get; set; }
        [JsonProperty("saleability")]
        public string Saleability { get; set; }
        [JsonProperty("isEbook")]
        public bool IsEbook { get; set; }
    }
    public class Epub
    {
        [JsonProperty("isAvailable")]
        public bool IsAvailable { get; set; }
    }
    public class Pdf
    {
        [JsonProperty("isAvailable")]
        public bool IsAvailable { get; set; }
    }
    public class AccessInfo
    {
        [JsonProperty("country")]
        public string Country { get; set; }
        [JsonProperty("viewability")]
        public string Viewability { get; set; }
        [JsonProperty("embeddable")]
        public bool Embeddable { get; set; }
        [JsonProperty("publicDomain")]
        public bool PublicDomain { get; set; }
        [JsonProperty("textToSpeechPermission")]
        public string TextToSpeechPermission { get; set; }
        [JsonProperty("epub")]
        public Epub Epub { get; set; }
        [JsonProperty("pdf")]
        public Pdf Pdf { get; set; }
        [JsonProperty("webReaderLink")]
        public string WebReaderLink { get; set; }
        [JsonProperty("accessViewStatus")]
        public string AccessViewStatus { get; set; }
        [JsonProperty("quoteSharingAllowed")]
        public bool QuoteSharingAllowed { get; set; }
    }
    public class SearchInfo
    {
        [JsonProperty("textSnippet")]
        public string TextSnippet { get; set; }
    }
    public class Item
    {
        [JsonProperty("kind")]
        public string Kind { get; set; }
        [JsonProperty("id")]
        public string Id { get; set; }
        [JsonProperty("etag")]
        public string Etag { get; set; }
        [JsonProperty("selfLink")]
        public string SelfLink { get; set; }
        [JsonProperty("volumeInfo")]
        public VolumeInfo VolumeInfo { get; set; }
        [JsonProperty("saleInfo")]
        public SaleInfo SaleInfo { get; set; }
        [JsonProperty("accessInfo")]
        public AccessInfo AccessInfo { get; set; }
        [JsonProperty("searchInfo")]
        public SearchInfo SearchInfo { get; set; }
    }
    public class RootObject
    {
        [JsonProperty("kind")]
        public string Kind { get; set; }
        [JsonProperty("totalItems")]
        public int TotalItems { get; set; }
        [JsonProperty("items")]
        public IList<Item> Items { get; set; }
    }
    public class APIHelper
    {
        public string Get(string uri)
        {
            HttpWebRequest request = (HttpWebRequest) WebRequest.Create(uri);
            request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
            using (Stream stream = response.GetResponseStream())
            using (StreamReader reader = new StreamReader(stream))
            {
                return reader.ReadToEnd();
            }
        }
        public Dictionary<string, string> DictionaryReturnData(string isbn)
        {
            string path = "https://www.googleapis.com/books/v1/volumes?q=isbn:" + isbn;
            string json = Get(path);
            Dictionary<string, string> responses  = new Dictionary<string, string>();
            var                        rootObject = JsonConvert.DeserializeObject<RootObject>(json);
            foreach (var obj in rootObject.Items )
            {
                responses.Add("Title", obj.VolumeInfo.Title);
                responses.Add("Description", obj.VolumeInfo.Description);
                responses.Add("Image", obj.VolumeInfo.ImageLinks.Thumbnail);
                responses.Add("Authors", string.Join(",", obj.VolumeInfo.Authors));    //list of strings
                responses.Add("Genre", string.Join(",", obj.VolumeInfo.Categories));   //list of strings
                responses.Add("Isbn", isbn);
                responses.Add("Publisher", obj.VolumeInfo.Publisher);
                responses.Add("PublishedDate", obj.VolumeInfo.PublishedDate);
                responses.Add("PageCount", obj.VolumeInfo.PageCount.ToString());
            }
            return responses;
        }
    }
}

我的书课:

    namespace BookstoreWeb.Models
{
    public class Book
    {
        public int Id { get; set; }
        [Required]
        public string Isbn { get; set; }
        [Required]
        public string Title { get; set; }
        public string Author { get; set; }
        public double Price { get; set; }
        [Required]
        public string Description { get; set; }
        public string Publisher { get; set; }
        public string PublishedDate { get; set; }
        public string PageCount { get; set; }
        public string Thumbnail { get; set; }
        public string Genre { get; set; }
    }
}

创建动作

 [HttpPost]
    public IActionResult Create(Book model)
    {
        APIHelper.APIHelper helper = new APIHelper.APIHelper();
        var responses = helper.DictionaryReturnData(model.Isbn);
        model.Author        = responses["Authors"];
        model.Genre         = responses["Genre"];
        model.Isbn          = responses["Isbn"];
        model.Price         = 10.00;
        model.Title         = responses["Title"];
        model.Description   = responses["Description"];
        model.Publisher     = responses["Publisher"];
        model.PublishedDate = responses["PublishedDate"];
        model.PageCount     = responses["PageCount"];
        model.Thumbnail     = responses["Image"];
        if (ModelState.IsValid) //check for validation
        {
            var newBook = new Book
            {
                Author        = model.Author,
                Genre         = model.Genre,
                Isbn          = model.Isbn,
                Price         = model.Price,
                Title         = model.Title,
                Description   = model.Description,
                Publisher     = model.Publisher,
                PublishedDate = model.PublishedDate,
                PageCount     = model.PageCount,
                Thumbnail     = model.Thumbnail,
            };
            newBook = _bookstoreData.Add(newBook);
            _bookstoreData.Commit();
            return RedirectToAction("Details", new {id = newBook.Id});
        }

从书类中删除注释[所需]似乎已经解决了问题。

最新更新