【问题标题】:View not detecting model property查看未检测模型属性
【发布时间】:2016-09-01 22:44:53
【问题描述】:

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

我不知道为什么它无法检测到我的类别 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?

【问题讨论】:

  • 只有在提交表单并返回视图时才会出现这种情况吗?
  • 它是ViewBag.Category_ID 而不是ViewBag.Categories。我错了吗?

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 2011-09-10
    • 2012-06-26
    • 1970-01-01
    • 2019-10-05
    • 2016-12-29
    • 1970-01-01
    相关资源
    最近更新 更多