查看未检测到模型属性

时间:2016-09-01 22:44:53

标签: c# entity-framework asp.net-mvc-4 ef-code-first

在我看来,我收到的错误是找不到我的类别ID。下面是浏览器中的错误图像。 browser error

我无法弄清楚为什么它无法检测到我的类别ID。这是我的观点。

@model SeniorProjectMVC.Models.ViewModels.ProductSkuViewModel

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

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Create</title>
</head>
<body>
    @using (Html.BeginForm()) 
    {
        @Html.AntiForgeryToken()

        <div class="container">
            <div class="text-center"> <h4>Create a new product</h4></div>
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="row">
                @*<div class="large-4 columns">
                    @Html.LabelFor(model => model.ID, htmlAttributes: new { @class = "control-label col-md-2" })
                    @Html.EditorFor(model => model.ID, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.ID, "", new { @class = "text-danger" })
                </div>*@

                <div class="large-6 columns">
                    @Html.LabelFor(model => model.PageURL, "Page URL", htmlAttributes: new { @class = "label" })
                    @Html.EditorFor(model => model.PageURL, new { htmlAttributes = new { @class = "" } })
                    @Html.ValidationMessageFor(model => model.PageURL, "", new { @class = "text-danger" })
                </div>

                <div class="large-6 columns">
                    @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "label" })
                    @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "" } })
                    @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="row">
                <div class="large-4 columns">
                    @Html.LabelFor(model => model.Code, htmlAttributes: new { @class = "label" })
                    @Html.EditorFor(model => model.Code, new { htmlAttributes = new { @class = "" } })
                    @Html.ValidationMessageFor(model => model.Code, "", new { @class = "text-danger" })
                </div>

                <div class="large-4 columns">
                    @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "label" })
                    @Html.TextAreaFor(model => model.Description, new { htmlAttributes = new { @class = "" } })
                    @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })

                </div>

                <div class="large-4 columns">
                    @Html.LabelFor(model => model.CategoryID, "Category", htmlAttributes: new { @class = "label" })
                    @Html.DropDownListFor(model => model.CategoryID, (SelectList)ViewBag.Categories, htmlAttributes: new { @class = "" })
                    @Html.ValidationMessageFor(model => model.CategoryID, "", new { @class = "text-danger" })

                </div>
            </div>

            <div class="row">
                <div class="large-4 columns">
                    @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "label" })
                    @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "" } })
                    @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
                </div>
                <div class="large-4 columns">
                    @Html.LabelFor(model => model.Featured, htmlAttributes: new { @class = "label" })
                    @Html.CheckBoxFor(model => model.Featured, htmlAttributes: new { @class = "" })
                </div>
                <div class="large-4 columns">
                    @Html.Label("Upload product image:", new { @class = "label" })
                    <input type="file" id="ProductImageUpload" name="ProductImageUpload" />
                </div>


            </div>

            <div class="text-center"> <h3> Sku information</h3> </div>
            <div class="row">
                <div class="large-6 columns end">
                    <label>Sku Name:</label>
                    @Html.TextBoxFor(model => model.SKUName, htmlAttributes: new {@class="form-field"})
                </div>
            </div>
            <div class="row">
                <div class="large-6 columns">
                    @Html.LabelFor(model => model.SKUDescription, "Sku Description", new { @class = "label" })
                    @Html.TextAreaFor(model => model.SKUDescription, htmlAttributes: new { @maxlength = 1000 })
                </div>
                <div class="large-3 columns">
                    <label>Sku Quantity</label>
                    @Html.EditorFor(model => model.SKUQuantity)
                </div>
                <div class="large-3 columns">
                    @Html.LabelFor(model => model.SKUInStock, "In stock")
                    @Html.CheckBoxFor(model => model.SKUInStock)
                </div>
            </div>
            <div class="row">
                <div class="large-6 columns text-center">
                    @Html.ActionLink("Back to List", "Index", null, htmlAttributes: new { @class = "button primary" })
                </div>
                <div class="large-6 columns text-center">                    
                        <input type="submit" value="Create" class="button primary" />                    
                </div>
            </div>
        </div>
    }


</body>
</html>

以下是视图的模型..

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

namespace SeniorProjectMVC.Models.ViewModels
{
    public class ProductSkuViewModel
    {
        #region Product Properties

        [Required]
        [MaxLength]
        public string PageURL { get; set; }

        [Required]
        [StringLength(250)]
        public string Name { get; set; }

        [Required]
        public string Code { get; set; }

        public string Description { get; set; }

        public int CategoryID { get; set; }

        [Column(TypeName = "money")]
        [DisplayFormat(DataFormatString = "{0:##.##}", ApplyFormatInEditMode = true)]
        public decimal Price { get; set; }

        [Required]
        public bool Featured { get; set; }

        #endregion

        #region Sku Properties

        [Required]
        [StringLength(250)]
        public string SKUName { get; set; }

        [MaxLength]
        public string SKUDescription { get; set; }

        public int SKUQuantity { get; set; }

        public bool SKUInStock { get; set; }

        #endregion


        //public Product product { get; set; }
        //public Sku sku { get; set; }
    }
}



 [HttpPost]
    [ValidateAntiForgeryToken]
    [Authorize(Roles = "SuperAdmin, Admin")]
    public ActionResult Create(Models.ViewModels.ProductSkuViewModel ProductSku)
    { 
    ModelState.Clear();
    //ProductSku.product.Category_ID = (Request["Category_ID"] != null ? Int32.Parse(Request["Category_ID"]) : 0);


    var SiteSettings = Models.Helpers.CacheManager.GetSiteSettings(db, "");

    #region nousedyet
    if (Request.Files.Count > 0)
    {
        for (int i = 0; i < Request.Files.Count - 1; i++)
        {

            var File = Request.Files[i];
            if (SiteSettings.First(ss => ss.Key == "AllowedProductImageTypes").Value.Split(',').Contains(Path.GetExtension(File.FileName)))
            {
                if (File.ContentLength > 0 &&
                    SiteSettings.First(ss => ss.Key.ToLower() == "allowedproductcontenttypes").Value.Split(',').Contains(File.ContentType))
                {
                    var imagePath = SiteSettings.First(ss => ss.Key.ToLower() == "productuploadpath");

                    if (!System.IO.Directory.Exists(imagePath.Value))
                        System.IO.Directory.CreateDirectory(Server.MapPath(imagePath.Value));

                        System.IO.File.Create(Server.MapPath(imagePath.Value + File.FileName));



                }
                else
                    ModelState.AddModelError("", "Could not create product, invalid product image");

            }
            else
            {
                ModelState.AddModelError("", "Could not create product, invalid file type");
            }
            //var FileName = Path.GetExtension(File.FileName);
        }
    }
    #endregion

    if (ModelState.IsValid)
    {
        //Populating the product model
        Product product = new Product();
        product.PageURL = ProductSku.PageURL;
        product.Name = ProductSku.Name;
        product.Code = ProductSku.Code;
        product.CategoryID = ProductSku.CategoryID;
        product.Description = ProductSku.Description;
        product.DateCreated = DateTime.Now;
        product.DateModified = DateTime.Now;

        db.Products.Add(product);
        db.SaveChanges();

        //Populating the sku model
        Sku sku = new Sku();
        sku.ProductID = product.ID;
        sku.Name = ProductSku.SKUName;
        sku.Description = ProductSku.SKUDescription;
        sku.Quantity = ProductSku.SKUQuantity;
        sku.InStock = ProductSku.SKUInStock;

        //set to one right now
        sku.PropertyID = 1;

        db.SKU_Table.Add(sku);
        db.SaveChanges();

    }

    ViewBag.Category_ID = new SelectList(db.Categories, "Category_ID", "CategoryName", ProductSku.Category_ID);
    return View(ProductSku);
}

那为什么不能找到我的类别ID?

1 个答案:

答案 0 :(得分:0)

我将属性更改为全部匹配,问题是我的名称与绑定上的categort id属性不匹配。