私はこのサイトのユーザーに同時に複数の画像をアップロードすることをお勧めします。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");
}
しかし、私はこの方法のためのビューを生成するとき、それは一つだけを操作する付ける機能を持っています(画像をアップロードできるように編集する必要があります):
@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'。イメージの配列が残っています。 –