목록 사이트에서 작업 중입니다. 이미지를 업로드하고 데이터베이스의 경로를 저장하려고합니다.ASP.NET MVC 4 양식에서 파일을 가져올 수 없습니다.
문제는 단지 양식에서 업로드 된 파일을 검색 할 수없는 것 같습니다. 여기
는@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Product</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</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>
<div class="editor-label">
@Html.LabelFor(model => model.Location)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Location)
@Html.ValidationMessageFor(model => model.Location)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Condition)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Condition)
@Html.ValidationMessageFor(model => model.Condition)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Price)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Price)
@Html.ValidationMessageFor(model => model.Price)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.PostedBy)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PostedBy)
@Html.ValidationMessageFor(model => model.PostedBy)
</div>
<div class="editor-field">
Upload Image <input type="file" name="listingImage" />
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
형태입니다 그리고 여기과 같이 설정을 나열하는 제품에 대한 나는 모델을 가지고 작업
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Product product, HttpPostedFileBase listingImage)
{
if (listingImage == null)
{
return RedirectToAction("Index");
}
if (ModelState.IsValid)
{
db.Products.Add(product);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(product);
}
입니다.
public class Product
{
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Location { get; set; }
public string Condition { get; set; }
public decimal Price { get; set; }
public string PostedBy { get; set; }
public string PhotoPath { get; set; }
}
양식을 완성하고 제출할 때마다 listingImage가 null이고 색인으로 돌아갑니다.
내가 누락 된 자료가 있습니까?
감사합니다.
고맙습니다.이 솔루션을 찾고있었습니다. – jaooe