我希望網站的用戶同時上傳多個圖片,最多可以添加5個圖片。我創建了我的所有ViewModels
以及[HttpPost]
方法,這些方法將處理圖像列表並遍歷每個圖像,從而保存圖像。是否可以使用表單在MVC中提交列表?
我的問題是:如何在Post方法中接收該列表?
我已經爲我的ViewModels下面的代碼:
public class ImageCreateViewModel
{
public int ImageId { get; set; }
public int CollectionId { get; set; }
[Required(ErrorMessage = "Please enter a description of the photo.")]
[MaxLength(50)]
public string Description { get; set; }
[Required(ErrorMessage = "Please attach an image.")]
public HttpPostedFileBase Image { get; set; }
public string Location { get; set; }
public int Order { get; set; }
}
public class ImagesCreateViewModel : ImageCreateViewModel
{
public List<ImageCreateViewModel> Images { get; set; }
public MyEnumerator GetEnumerator()
{
return new MyEnumerator(this);
}
}
public class MyEnumerator
{
int index;
ImagesCreateViewModel imagesCreateViewModel;
public MyEnumerator(ImagesCreateViewModel imagesCreateViewModel)
{
this.imagesCreateViewModel = imagesCreateViewModel;
index = -1;
}
public bool MoveNext()
{
index++;
return (index < imagesCreateViewModel.Images.Count());
}
public ImageCreateViewModel Current
{
get
{
return (imagesCreateViewModel.Images[index]);
}
}
}
這裏是我的控制器:
[HttpPost]
public ActionResult CreateImages(ImagesCreateViewModel imagesEditViewModel)
{
if (!ModelState.IsValid)
{
return View(imagesEditViewModel);
}
foreach (ImageCreateViewModel imageCreateViewModel in imagesEditViewModel)
{
string fileName = Guid.NewGuid().ToString();
string serverPath = Server.MapPath("~");
string contentPath = String.Format("Content\\{0}\\Content\\Images\\{1}", Helper.Helper.ResolveBrand(), fileName);
string imagePath = serverPath + contentPath;
bool success = Helper.Helper.SaveImage(imagePath, imageCreateViewModel.Image.InputStream);
if (success)
{
Image image = new Image
{
Collection = ds.Single<Collection>(c => c.CollectionId == imageCreateViewModel.CollectionId),
Description = imageCreateViewModel.Description,
Location = contentPath,
Order = Helper.Helper.GetImageOrder(imageCreateViewModel.CollectionId)
};
ds.InsertOnSubmit<Image>(image);
ds.SubmitChanges();
}
else
//TODO: Write Error to them
success = false;
}
return RedirectToAction("Collection");
}
但是,當我生成這個方法的視圖它不僅具有abilty操縱一個
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>New Image</legend>
<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.Image)
</div>
<div class="editor-field">
@Html.ValidationMessageFor(model => model.Image)
<input type="file" name="Image" />
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
不知道是不是ASP明確,但一般可以通過陣列在POST中通過將字段的名稱設置爲數組標識符。即''這工作在PHP和我想你可以做同樣的或類似的ASP。想想你是否編碼過 - 你會有'Image [] = someImage; Image [] = someOtherImage; Image [] = anotherImage',它爲您留下一系列圖像。 – 2012-03-13 17:50:59