アプリケーションに画像をアップロードしようとしていますが、常に null が返されます。ここで問題を見つけることができません。あなたは私を助けることができます?これが私のコードです。
モデル
[Table("Slider")]
public partial class Slider : BaseModel
{
[Required]
[StringLength(200)]
public string FileName { get; set; }
[StringLength(200)]
public string Title { get; set; }
[StringLength(1000)]
public string Description { get; set; }
public int? Order { get; set; }
}
[NotMapped]
public class SliderImage : Slider
{
public HttpPostedFileBase ImageFile { get; set; }
}
意見
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="modal-body">
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Id)
<div class="form-group">
@Html.LabelFor(model => model.FileName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.FileName, new { @class = "form-control", @readonly = "readonly" })
@Html.ValidationMessageFor(model => model.FileName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ImageFile, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.ImageFile, new { @class = "form-control", type = "file" })
//This is Same as below
//<input class="form-control" id="ImageFile" name="ImageFile" type="file" value="">
</div>
</div>
コントローラ
public ActionResult Edit(int id)
{
Slider slider = _db.Sliders.Find(id);
if (slider == null)
{
return HttpNotFound();
}
Mapper.CreateMap<Slider, SliderImage>();
SliderImage sliderImage = Mapper.Map<Slider, SliderImage>(slider);
return PartialView("_Edit", sliderImage);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditSlider([Bind(Include = "Id,FileName,Title,Description,Order,IsActive,Name,ImageFile")] SliderImage sliderImage)
{
if (ModelState.IsValid)
{
Mapper.CreateMap<SliderImage, Slider>();
Slider slider = Mapper.Map<SliderImage, Slider>(sliderImage);
_db.Entry(slider).State = EntityState.Modified;
_db.SaveChanges();
return Json(new { success = true });
}
return PartialView("_EditSlider");
}
私はこれで何が間違っていますか?
問題を発見
ブートストラップモーダルポップアップ内に部分ビューをバインドしています。ポップアップからアップロードすると、アップロードが null を返します。代わりに、ブラウザーで部分ビューを直接開くと、ファイルがモデルに存在します。したがって、ファイルのアップロードに問題はありません。問題はモーダルポップアップか何かにあります。
Bootstrap モデルを使用する場合
部分的な View Direct を使用する場合
次の画像で、Bootstrap Modal Submit と Using the partial View Directly の間でフィドラーを使用したときに見つかった違いをそれぞれ確認してください
モーダルポップアップから投稿する場合、コンテンツタイプはapplication/x-www-form-urlencoded
、部分ビューを直接使用する場合と同じように変更されますmultipart/form-data
根本的な問題が見つかりました。
$('form', dialog).submit(function () {
var $form = $(this);
var enctype = $form.attr('id');
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (result) {
if (result.success) {
$('#myModal').modal('hide');
//Refresh
location.reload();
} else {
$('#myModalContent').html(result);
bindForm();
}
}
});
return false;
});
AJAX 投稿を使用して、フォームからデータを送信しています。ajaxを使用$(this).serialize()
すると成功が呼び出されますが、コンテンツタイプが異なるためファイルが返されません。どうすればこれを変更できますか??