【问题标题】:Uploading Image in Asp.net mvc Controller在 Asp.net mvc 控制器中上传图像
【发布时间】:2017-10-20 23:54:10
【问题描述】:

我有一个上传表单,我想传递我的信息,例如图像和其他一些字段,但我不知道如何上传图像..

这是我的模型类

public partial class NewProductCategory
    {   
        public string ProductName { get; set; }
        public string ProductDescription { get; set; }
        public string ProductPrice { get; set; }
        public string ProductImage { get; set; }
        public string ProductQuantity { get; set; }
        public Nullable<bool> ProductStatus { get; set; }
        public Nullable<int> CategoryId { get; set; }

        public HttpPostedFileBase user_image_data { get; set; }
        public virtual Category Category { get; set; }

    }
[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(NewProductCategory productcategory, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                ProductCategory newproductCategory = new ProductCategory();
                string path = Path.Combine(Server.MapPath("~/Content/files"), Path.GetFileName(file.FileName));
                file.SaveAs(path);
                newproductCategory.ProductDescription = productcategory.ProductDescription;
                newproductCategory.ProductQuantity = productcategory.ProductQuantity;
                newproductCategory.ProductStatus = productcategory.ProductStatus;

                newproductCategory.CategoryId = productcategory.CategoryId;

                db.ProductCategories.Add(newproductCategory);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "Name", productcategory.CategoryId);
            return View(productcategory);
        }

这是我的查看代码

@model MvcApplication1.Models.NewProductCategory
@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>ProductCategory</legend>
        @using (Html.BeginForm("Create", "Temp", FormMethod.Post, new { enctype = "multipart/form-data" }))

        {
        <div class="editor-label">
            @Html.LabelFor(model => model.ProductName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ProductName)
            @Html.ValidationMessageFor(model => model.ProductName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ProductDescription)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ProductDescription)
            @Html.ValidationMessageFor(model => model.ProductDescription)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ProductPrice)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ProductPrice)
            @Html.ValidationMessageFor(model => model.ProductPrice)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.user_image_data)
        </div>
        <div class="editor-label">
               @Html.Label("Upload your image")
            </div>


            <div class="editor-field">
                @Html.TextBoxFor(model => model.user_image_data, new { Type = "File" })
                @Html.ValidationMessageFor(model => model.user_image_data)
            </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ProductQuantity)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ProductQuantity)
            @Html.ValidationMessageFor(model => model.ProductQuantity)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ProductStatus)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ProductStatus)
            @Html.ValidationMessageFor(model => model.ProductStatus)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.CategoryId, "Category")
        </div>
        <div class="editor-field">
            @Html.DropDownList("CategoryId", String.Empty)
            @Html.ValidationMessageFor(model => model.CategoryId)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
        }
    </fieldset>
}

请在控制器中帮助我,我正在为文件上传获取 Null 值

【问题讨论】:

  • 因为你绑定了一个名为user_image_data(不是file)的属性,但是你没有展示你的模型,所以我们不知道那是什么——它应该是typeof HttpPostedFileBase。如果要绑定到名为 file 的参数,则将输入命名为 file
  • 是的,我已经使用 HttpPostedFileBase,但是我有一个问题,@model MvcApplication1.Models.NewProductCategory 没有这个它可以运行,但是另一个输入字段它会出错,好的,我将编辑并添加我的模型也上课

标签: asp.net-mvc razor file-upload httppostedfilebase


【解决方案1】:

更新答案

@model MvcApplication1.Models.NewProductCategory
@{
    ViewBag.Title = "Create";
}

   @using (Html.BeginForm("Create", "Temp", FormMethod.Post, new { enctype = "multipart/form-data" }))

    {    @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)

<fieldset>
    <legend>ProductCategory</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.ProductName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ProductName)
            @Html.ValidationMessageFor(model => model.ProductName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ProductDescription)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ProductDescription)
            @Html.ValidationMessageFor(model => model.ProductDescription)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ProductPrice)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ProductPrice)
            @Html.ValidationMessageFor(model => model.ProductPrice)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.user_image_data)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.user_image_data, new { Type = "File" })
            @Html.ValidationMessageFor(model => model.user_image_data)
        </div>
        <div class="editor-label">
            @Html.Label("Upload your image")
        </div>
        <div class="editor-label">
            @Html.TextBox("file",null,htmlAttributes: new { Type = "file" })
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.ProductQuantity)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ProductQuantity)
            @Html.ValidationMessageFor(model => model.ProductQuantity)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ProductStatus)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ProductStatus)
            @Html.ValidationMessageFor(model => model.ProductStatus)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.CategoryId, "Category")
        </div>
        <div class="editor-field">
            @Html.DropDownList("CategoryId", String.Empty)
            @Html.ValidationMessageFor(model => model.CategoryId)
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>  
      </fieldset>
    }

【讨论】:

  • 我已经完成了上述更改,仍然在 httpHttpPostedFileBase 文件中获得空值,现在还有什么其他选项,我只是创建了一个没有模型类的演示,它可以正常工作,但是在添加和传递时(NewProductCategory productcategory, HttpPostedFileBase 文件)这个类得到空值
猜你喜欢
  • 2012-05-11
  • 2016-12-13
  • 2012-02-28
  • 2020-12-24
  • 2011-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-16
相关资源
最近更新 更多