模型作为null传入

时间:2015-07-24 00:37:30

标签: asp.net-mvc-5 entity-framework-6

当尝试向数据库添加新项目时(使用EF 6.1.3和MVC 5),我的模型始终作为null传递。这是模特:

using System.ComponentModel.DataAnnotations;
using System.Web;

namespace AccessorizeForLess.ViewModels
{
    public class AddNewProductViewModel
    {
        public string Name { get; set; }

        [DataType(DataType.MultilineText)]
        public string Description { get; set; }

        public decimal Price { get; set; }

        public string AltText { get; set; }

        public int Quantity { get; set; }

        public string ImagePath { get; set; }

        public HttpPostedFileBase Image { get; set; }
    }
}

这是控制器中的创建方法(我知道它已经被扼杀了但我会在那里工作)

    [HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include="ProductImageId,ProductName,ProductPrice,Quantity,ProductDescription")] AddNewProductViewModel model)
{
    ProductImageViewModel image = new ProductImageViewModel() { ImagePath = "/Content/ProductImages/" + model.Image.FileName, AltText = model.AltText };
    ProductImage newImage = new ProductImage() { ImagePath = image.ImagePath, AltText = image.AltText };
    entities.ProductImages.Add(newImage);
    await entities.SaveChangesAsync();
    int ImageId = newImage.ProductImageId;

    Product product = new Product()
    {
        ProductImageId = ImageId,
        ProductName = model.Name,
        ProductDescription = model.Description,
        ProductPrice = model.Price,
        Quantity = model.Quantity
    };

    string file = model.Image.FileName;
    string path = @"/Content/ProductImages/";

    model.Image.SaveAs(path + file);

    if (ModelState.IsValid)
    {
        entities.Products.Add(product);
        await entities.SaveChangesAsync();
        return RedirectToAction("Index");
    }

    ViewBag.ProductImageId = new SelectList(entities.ProductImages, "ProductImageId", "ImagePath", product.ProductImageId);
    return View("Index");
}

观点:

 @model AccessorizeForLess.ViewModels.AddNewProductViewModel

@{
    ViewBag.Title = "Add New Product";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm("Create", "Products", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Product</h4>
        <hr />
        @Html.ValidationSummary(true)
        <div class="form-group">
            @Html.LabelFor(model => model.ImagePath, "Image", new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.ImagePath, new { @type = "file" })
                @Html.ValidationMessageFor(model => model.ImagePath)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.AltText, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.AltText)
                @Html.ValidationMessageFor(model => model.AltText)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Price, new { @class = "control-label col-md-2" ,@width = "25px"})
            <div class="col-md-10">
                @Html.EditorFor(model => model.Price)
                @Html.ValidationMessageFor(model => model.Price)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Quantity, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Quantity)
                @Html.ValidationMessageFor(model => model.Quantity)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Description, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Description, new { @cols = "25", @rows = "55" })
                @Html.ValidationMessageFor(model => model.Description)
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

我做错了什么?

修改 @Stephen当我试图再次运行它来回答你的问题时我得到了这个

enter image description here

并且不知道这是什么,你以前见过这样的事吗?

修改

错误是

描述:编译服务此请求所需的资源时发生错误。请查看以下特定错误详细信息并相应地修改源代码。

  

编译器错误消息:CS1520:方法必须具有返回类型

     

来源错误:

     

第641行:#line默认第642行:#line hidden   第643行:BeginContext(&#34;〜/ Views / Products / Create.cshtml&#34;,2361,9,   真正);第644行:第645行:WriteLiteral(&#34; \ r \ n \ r \ n \ n&#34;);

     

源文件:c:\ Users \ Richard \ AppData \ Local \ Temp \ Temporary ASP.NET   文件\ VS \ 014a60c5 \ fc8be8b2 \ App_Web_create.cshtml.c6727781.vcyi,_hh.0.cs   行:643

修改

我有这个用于上传图片:

string file = model.Image.FileName;
string path = Server.MapPath(@"~/Content/ProductImages");
 model.Image.SaveAs(path + "/" + file);

我已经介入了它并且所有变量都是正确的,它将数据添加到数据库但是没有保存图像,我不能为我的生活找出原因?

1 个答案:

答案 0 :(得分:0)

替换:

public async Task<ActionResult> Create([Bind(Include="ProductImageId,ProductName,ProductPrice,Quantity,ProductDescription")] AddNewProductViewModel model)

使用:

public async Task<ActionResult> Create([Bind(Include = "ImagePath,AltText,Name,Price,Quantity,Description")] AddNewProductViewModel model)