카테고리가 완벽하게 저장되어있는 내 DB에 범주 객체를 저장하고 내가이 범주를 참조 할 수 있습니다 이미지를 업로드하려고하지만 이미지가되고 있지 않습니다 파일을 업로드 어떤 이유로 업로드되었습니다. 내가 디버깅 할 때 내 응용 프로그램이 "Null"파일이기 때문에 서버에 파일을 저장하는 메서드를 입력하지 않는 것을 볼 수 있습니다.
모델 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace SkyLearn.Areas.Categories.Models
{
public class Category
{
public int ID { get; set; }
public string Title { get; set; }
public string Icon { get; set; }
public string Description { get; set; }
}
public class CategoryDBContext : DbContext
{
public DbSet<Category> categories { get; set; }
}
}
컨트롤러 :
//
// POST: /Categories/Category/Create
[Authorize(Roles = "administrator")]
[HttpPost]
public ActionResult Create(Category category, HttpPostedFileBase Icon)
{
if (Icon != null && Icon.ContentLength > 0)
{
// extract only the filename
var fileName = Path.GetFileName(Icon.FileName);
// store the file inside ~/App_Data/uploads folder
var path = Path.Combine(Server.MapPath("../Content/icons/"), fileName);
Icon.SaveAs(path);
category.Icon = fileName;
}
if (ModelState.IsValid)
{
db.categories.Add(category);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(category);
}
보기 : 실 거예요 파일 ID가 감사 저장하는 방법을 입력 이유를 말해 줄 수
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.master" Inherits="System.Web.Mvc.ViewPage<SkyLearn.Areas.Categories.Models.Category>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Create
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Create</h2>
<script src="<%: Url.Content("~/Scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") %>" type="text/javascript"></script>
<% using (Html.BeginForm()) { %>
<form action="" method="post" enctype="multipart/form-data">
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Category</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.Title) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.Title) %>
<%: Html.ValidationMessageFor(model => model.Title) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Icon) %>
</div>
<div class="editor-field">
<input type="file" name="icon" id="icon"/>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Description) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.Description) %>
<%: Html.ValidationMessageFor(model => model.Description) %>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
</form>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>
사람.
나는 stackoverflow에 다른 답변을 살펴 보았습니다. 그리고 그들 중 일부는 저와 같은 문제가 있었지만 해결책은 없었습니다.
또한 내 config.web에서 업로드 크기 등을 변경하려고했습니다.
도와주세요 :)
내가 브라우저가 중첩 된 양식을 처리 할 방법을 모르는,하지만 수동으로'Html.BeginForm()'후 작성하는 폼 태그를 사용하여 아무 문제가 없습니다. 'Html.BeginForm()'은 Html 속성을 출력하기 위해 과부하가 있습니다 :'Html.BeginForm (action, controller, FormMethod.Post, new {enctype = "multipart/form-data"})'. –
답장을 보내 주시면 즉시 답변 드리겠습니다. 그저 모든 문제를 해결했습니다. :) – AronChan
감사합니다. 다행했습니다. :) –