Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 对象引用未设置为asp.net mvc中对象的实例_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

Asp.net mvc 对象引用未设置为asp.net mvc中对象的实例

Asp.net mvc 对象引用未设置为asp.net mvc中对象的实例,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,我正在创建属于类别模型的新产品 “模型”对象上的“对象引用未设置为对象实例” 我的控制器类: public ActionResult CreateProduct() { SetCateProductViewBag(); return View(new CateProdViewModel()); } [HttpPost] public ActionResult CreateProduct(CateProdViewModel model) { var

我正在创建属于类别模型的新产品

“模型”对象上的“对象引用未设置为对象实例”

我的控制器类:

public ActionResult CreateProduct()   
{
    SetCateProductViewBag();
    return View(new CateProdViewModel());
}

[HttpPost]       
public ActionResult CreateProduct(CateProdViewModel model)
{

    var ValidImageTypes = new String[]
    {
        "image/gif",
        "image/jpeg",
        "image/jpg",
        "image/pjpeg",
        "image/png"
    };
    if (model.ImageUpload == null || model.ImageUpload.ContentLength == 0)
    {
        ModelState.AddModelError("ImageUpload", "This Field is required.");
    }
    else if (!ValidImageTypes.Contains(model.ImageUpload.ContentType))
    {
        ModelState.AddModelError("ImageUload", "Please choose either a GIF,jpg or png type of file.");
    }
    if (ModelState.IsValid)
    {

        var prod = new Product
        {
            //   CategoryName =model.category.CategoryName,
            //CategoryDescription=model.category.CategoryDescription,

            //CategoryId=model.CategoryId,
            ProductName = model.ProductName,
            ProductDescription = model.ProductDescription,
            Model = model.Model,
            ProductPrice = model.ProductPrice,
            AvailableForSale = model.AvailableForSale,
            Shippable = model.Shippable,
            AvailableStock = model.AvailableStock,
            ProductPicture = model.ProductPicture

        };
        SetCateProductViewBag(prod.CategoryId);
        if (model.ImageUpload != null && model.ImageUpload.ContentLength > 0)
        {
            var UploadDir = "~/Uploads";
            var ImagePath = Path.Combine(Server.MapPath(UploadDir), model.ImageUpload.FileName);
            var ImageUrl = Path.Combine(UploadDir, model.ImageUpload.FileName);
            model.ImageUpload.SaveAs(ImagePath);
            prod.ProductPicture = ImageUrl;
        }
        productContext.product.Add(prod);
        productContext.SaveChanges();
        return RedirectToAction("CategoryIndex");
    }

    return View(model);
}
CateProdViewModel类:

public class CateProdViewModel
{
    public int CategoryId { get; set; }
    public string CategoryName { get; set; }
    public string CategoryDescription { get; set; }
    public string ProductName { get; set; }
    public string ProductDescription { get; set; }
    public int AvailableStock { get; set; }
    public decimal ProductPrice { get; set; }
    public string Model { get; set; }
    public bool AvailableForSale { get; set; }
    public bool Shippable { get; set; }
    [DataType(DataType.ImageUrl)]
    public string ProductPicture { get; set; }
    public int SelectedValue { get; set; }
    [DataType(DataType.Upload)]
    public HttpPostedFileBase ImageUpload { get; set; }
    public virtual Category category { get; set; }
    [Display(Name="Product Categories")]
    public virtual ICollection<Category> categories { get; set; }
}
公共类CateProdViewModel
{
public int CategoryId{get;set;}
公共字符串CategoryName{get;set;}
公共字符串类别描述{get;set;}
公共字符串ProductName{get;set;}
公共字符串ProductDescription{get;set;}
public int AvailableStock{get;set;}
公共十进制产品价格{get;set;}
公共字符串模型{get;set;}
public bool AvailableForSale{get;set;}
公共布尔可交付{get;set;}
[数据类型(DataType.ImageUrl)]
公共字符串ProductPicture{get;set;}
public int SelectedValue{get;set;}
[数据类型(DataType.Upload)]
公共HttpPostedFileBase图像上载{get;set;}
公共虚拟类别{get;set;}
[显示(Name=“产品类别”)]
公共虚拟ICollection类别{get;set;}
}
类别和产品的实体类:

public class Product
{
    public int ProductId { get; set; }
    public string ProductName { get; set; }
    public string ProductDescription { get; set; }
    public int AvailableStock { get; set; }
    public decimal ProductPrice { get; set; }
    public string Model { get; set; }
    public bool AvailableForSale { get; set; }
    public bool Shippable { get; set; }
    public string ProductPicture { get; set; }
    public int CustomerId { get; set; }
    public int CategoryId { get; set; }
    public virtual Category category { get; set; }

}

 public class Category
{
    public int CategoryId { get; set; }
    public string CategoryName { get; set; }
    public string CategoryDescription { get; set; }
    public virtual ICollection<Product> product { get; set; }

}
公共类产品
{
public int ProductId{get;set;}
公共字符串ProductName{get;set;}
公共字符串ProductDescription{get;set;}
public int AvailableStock{get;set;}
公共十进制产品价格{get;set;}
公共字符串模型{get;set;}
public bool AvailableForSale{get;set;}
公共布尔可交付{get;set;}
公共字符串ProductPicture{get;set;}
public int CustomerId{get;set;}
public int CategoryId{get;set;}
公共虚拟类别{get;set;}
}
公共类类别
{
public int CategoryId{get;set;}
公共字符串CategoryName{get;set;}
公共字符串类别描述{get;set;}
公共虚拟ICollection产品{get;set;}
}
我的看法是:

@model SmartShoppingCart.Models.CateProdViewModel

@{
  ViewBag.Title = "CreateProduct";
}

<h2>Create Product</h2>

<form action="" method="post" enctype="multipart/form-data">
    <div>
        @Html.LabelFor(m=>m.CategoryId,"Category")
        @Html.DropDownList("CategoryId",ViewBag.categories as SelectList, string.Empty)
        @Html.ValidationMessageFor(m=>m.CategoryId)
    </div>
    <div>
        @Html.HiddenFor(m=>m.CategoryId)
    </div>
    <div>
        @Html.LabelFor(m=>m.ProductName)
        @Html.TextBoxFor(m=>m.ProductName)
        @Html.ValidationMessageFor(m=>m.ProductName)
    </div>
     <div>
        @Html.LabelFor(m=>m.ProductDescription)
        @Html.TextBoxFor(m=>m.ProductDescription)
        @Html.ValidationMessageFor(m=>m.ProductDescription)
    </div>
     <div>
        @Html.LabelFor(m=>m.Model)
        @Html.TextBoxFor(m=>m.Model)
        @Html.ValidationMessageFor(m=>m.Model)
    </div>
    <div>
        @Html.LabelFor(m=>m.ProductPrice)
        @Html.TextBoxFor(m=>m.ProductPrice)
        @Html.ValidationMessageFor(m=>m.ProductPrice)
    </div>
     <div>
        @Html.LabelFor(m=>m.AvailableForSale)
        @Html.TextBoxFor(m=>m.AvailableForSale)
        @Html.ValidationMessageFor(m=>m.AvailableForSale)
    </div>
     <div>
        @Html.LabelFor(m=>m.Shippable)
        @Html.TextBoxFor(m=>m.Shippable)
        @Html.ValidationMessageFor(m=>m.Shippable)
    </div>
     <div>
        @Html.LabelFor(m=>m.AvailableStock)
        @Html.TextBoxFor(m=>m.AvailableStock)
        @Html.ValidationMessageFor(m=>m.AvailableStock)
    </div>
    <div>
        @Html.LabelFor(m=>m.ImageUpload)
        @Html.TextBoxFor(m => m.ImageUpload, new {type="file" })
    </div>
    <div>
         <button type="submit" value="Add" ></button>
    </div>
</form>

private void SetCateProductViewBag(int? CateId = null)
{
    if (CateId == null)
    {
        ViewBag.Categories = new SelectList(productContext.category, "CategoryId", "CategoryName");
    }
    else
    {
        ViewBag.Categories = new SelectList(productContext.category.ToArray(),"CategoryId","CategoryName",CateId);
    }
}
@model SmartShoppingCart.Models.CateProdViewModel
@{
ViewBag.Title=“CreateProduct”;
}
创造产品
@LabelFor(m=>m.CategoryId,“Category”)
@Html.DropDownList(“CategoryId”,ViewBag.categories作为SelectList,string.Empty)
@Html.ValidationMessageFor(m=>m.CategoryId)
@Html.HiddenFor(m=>m.CategoryId)
@Html.LabelFor(m=>m.ProductName)
@Html.TextBoxFor(m=>m.ProductName)
@Html.ValidationMessageFor(m=>m.ProductName)
@LabelFor(m=>m.ProductDescription)
@Html.TextBoxFor(m=>m.ProductDescription)
@Html.ValidationMessageFor(m=>m.ProductDescription)
@LabelFor(m=>m.Model)
@Html.TextBoxFor(m=>m.Model)
@Html.ValidationMessageFor(m=>m.Model)
@LabelFor(m=>m.ProductPrice)
@Html.TextBoxFor(m=>m.ProductPrice)
@Html.ValidationMessageFor(m=>m.ProductPrice)
@LabelFor(m=>m.AvailableForSale)
@Html.TextBoxFor(m=>m.AvailableForSale)
@Html.ValidationMessageFor(m=>m.AvailableForSale)
@LabelFor(m=>m.Shippable)
@Html.TextBoxFor(m=>m.Shippable)
@Html.ValidationMessageFor(m=>m.Shippable)
@LabelFor(m=>m.AvailableStock)
@Html.TextBoxFor(m=>m.AvailableStock)
@Html.ValidationMessageFor(m=>m.AvailableStock)
@LabelFor(m=>m.ImageUpload)
@TextBoxFor(m=>m.ImageUpload,新的{type=“file”})
私有无效SetCateProductViewBag(int?CateId=null)
{
if(CateId==null)
{
ViewBag.Categories=新的选择列表(productContext.category,“CategoryId”,“CategoryName”);
}
其他的
{
ViewBag.Categories=新的选择列表(productContext.category.ToArray(),“CategoryId”,“CategoryName”,CateId);
}
}

为什么不将第一个操作更改为:

public ActionResult CreateProduct(CateProdViewModel model)   
{
    SetCateProductViewBag();
    return View(model);
}
您的
DefaultModelBinder
将实例化该模型,因此它永远不会为空


除此之外,我需要知道当我执行Create方法时,您所说的
是什么意思。因为这里没有
Create
方法。你有
CreateProduct
,你有两个。这是一个
POST
GET
请求吗?

您的错误发生是因为您的模型在回发时为空。该模型为空,因为它包含名为
model
的属性,并且操作方法的参数名也被命名为
model
,这会混淆较差的
DefaultModelBinder
。请将属性的名称更改为其他名称,或将post操作方法中的参数名称更改为其他名称。

出现错误的行是什么。这通常就是你在这一行看到错误的方式:如果(model.ImageUpload==null | | model.ImageUpload.ContentLength==0)model必须为null,或者ContentLength不是intYes,我说的是CreateProduct操作方法。通过在get-post请求中设置相同的参数,调用错误。它没有被处决,现在可以正常工作了。但是现在我得到了产品图片的空值,category。而CategoryId和ProductId的值为0。错误发生在“INSERT语句与外键约束“FK_Product_Category1”冲突。冲突发生在数据库“ShoppingCart\”、表“dbo.Category\”、列“CategoryId”中。\r\n语句已终止。}视图中还有其他错误-我将检查并更新CategoryId的应答,使用
@Html.DropDownListFor(m=>m.CategoryId,ViewBag.categories作为SelectList)
并删除
@Html.HiddenFor(m=>m.CategoryId)
。至于ProductId和ProductPicture,您似乎没有为这些属性呈现任何类型的
input
select