输入标记在Create ActionResult中返回Null到Model

时间:2017-09-23 21:27:15

标签: html asp.net-mvc repository

在像这样的实体中,我将名为ImageFile的HttpPostedFileBase设置为虚拟,因为我使用映射

  public class Slider : IEntity
 {
      public virtual int Id { get; set; }

      public virtual HttpPostedFileBase ImageFile { get; set; }

   }

这里实体im获取entity.ImageFile = null,因此低于动作代码

 [HttpPost]
      public ActionResult AddOrEdit(Slider entity)
    {
        try
        {
            string fileName = Path.GetFileNameWithoutExtension(entity.ImageFile.FileName);
            string extension = Path.GetExtension(entity.ImageFile.FileName);
            fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension;
            entity.Image = fileName;
            fileName = Path.Combine(Server.MapPath("~/Image/"), fileName);
            entity.ImageFile.SaveAs(fileName);
            if (ModelState.IsValid)
            {
              if (entity.Id == 0)
                {

                    _sliderService.Insert(entity);
                }

在视图中我添加了名为ImageFile的输入标记,所以通常我不应该为null ??

请帮助

   @model Slider
 @{

     Layout = null;
 }

 @using (Html.BeginForm("AddOrEdit", "Slider", FormMethod.Post, new { id = 
 "form", enctype = "multipart/form-data" , onsubmit = "return 
   SubmitForm(this)" }))
  {
        @Html.HiddenFor(m => m.Id)



    <div class="form-group" style="height:270px;">
       @Html.LabelFor(m => m.ImageFile, new { @class = "blue-text", @style = 
      "font-size:16px", @id = "" })

    <input name="ImageFile" type="file"  />

</div>

<div class="form-group">
    <input type="submit" value="Submit" class="btn btn-primary" />
    <input type="reset" value="Reset" class="btn" />
</div>

}

2 个答案:

答案 0 :(得分:0)

为了获取文件,您可以更改动作参数,如下所示:

[HttpPost]
public ActionResult AddOrEdit(HttpPostedFileBase ImageFile,Slider slider)
{
....
}

但您还需要像这样更改您的实体:

public class Slider : IEntity
{
   public virtual int Id { get; set; }
   public virtual string ImageUrl { get; set; }
}

然后在你可以做的行动中:

if (ImageFile!= null)
        {
            slider.ImgUrl = SaveImage(ImageFile);
            db.Slider.Add(slider);
            db.SaveChanges();

        }

和SaveImage方法:

 private string SaveImage(HttpPostedFileBase uploadFile)
        {
            if (uploadFile != null && uploadFile.ContentLength > 0)
            {
                string relativePath = "~/Image/" + Path.GetFileName(uploadFile.FileName);
                string physicalPath = Server.MapPath(relativePath);
                uploadFile.SaveAs(physicalPath);
                return uploadFile.FileName;
            }
            return null;
        }

答案 1 :(得分:0)

谢谢你们的帮助,我通过删除&#34; onsubmit =&#34;返回SubmitForm(this)&#34;来自Html.BeginForm标签,但在这种情况下,我没有得到验证消息或通知消息的任何想法???

以下是onsubmit的代码

function SubmitForm(form) {
        $.validator.unobtrusive.parse(form);
        if ($(form).valid()) {
            $.ajax({
                type: "POST",
                url: form.action,
                data: $(form).serialize(),
                success: function (data) {
                    if (data.success) {
                        Popup.dialog('close');
                        dataTable.ajax.reload();

                        $.notify(data.message, {
                            globalPosition: "top center",
                            className: "success"
                        })

                    } else {
                        Popup.dialog('close');

                        $.notify(data.message, {
                            globalPosition: "top center",
                            className: "error"
                        })
                    }
                }
            });
        }
        return false;
    }