簡體   English   中英

Asp.net MVC-fileupload值在httppost方法中顯示空值

[英]Asp.net MVC-fileupload value shows null values in httppost method

在視圖上:

 <div id="project-contact-form"> @using (Ajax.BeginForm("Apply","Careers", new AjaxOptions { OnSuccess = "onApplySuccess" }, new { @id = "form1", @enctype = "multipart/form-data" })) { <div class="row"> <div class="col-md-12"> <div id="success-project-contact-form" class="no-margin-lr"></div> </div> <div class="col-md-6"> <input type="text" required name="Firstname" id="Firstname" placeholder="First name" class="big-input"> </div> <div class="col-md-6"> <input type="text" required name="Lastname" id="Lastname" placeholder="Last name" class="big-input"> </div> <div class="col-md-6"> <input type="email" required name="Email" id="email" placeholder="E-mail" class="big-input"> </div> <div class="col-md-6"> <input type="text" required name="Mobile" id="mobile" placeholder="Mobile" class="big-input"> </div> <div class="col-md-6"> <input type="file" name="FileUploader" id="files" class="hidden" /> <label for="files" class="float-left cursor-pointer">Upload CV</label> </div> <div class="col-md-12 text-center"> <button id="project-contact-us-button" type="submit" class="btn btn-medium btn-transparent-bnsights btn-rounded md-margin-15px-bottom sm-display-table sm-margin-lr-auto">Send</button> </div> </div> } </div> 

在控制器中: HttpPostedFileBase FileUploader value = null我嘗試了幾種方法,但是不知道為什么它為null

 public ActionResult Apply(ContactUsModel model, HttpPostedFileBase FileUploader)
    {
        SendEmail sendemail = new SendEmail();

        string toEmail = ConfigurationManager.AppSettings["ContactUsEmail"];

        var keys = new Dictionary<string, string>() {
            { "Firstname", model.Firstname },
            { "Lastname", model.Lastname },
            { "Email", model.Email },
            { "Orgnization", model.Orgnization },
            { "Message", model.Message }
        };

        // string body = $"Firstname : {model.Firstname} \n Lastname : {model.Lastname}\n Email : {model.Email} \n Orgnization : {model.Orgnization} \n Message : {model.Message}";
        if (keys != null && keys.Count != 0)
        {
            string body = string.Join(Environment.NewLine, keys.Select(x => $"{x.Key}: {x.Value}"));

            sendemail.Send(new EmailModel()
            {
                Body = body,
                Subject = "Contact Us Message",
                To = new List<string>() { toEmail },


            }, FileUploader);

            return Json(new { val = true }, JsonRequestBehavior.AllowGet);
        }
        else
        {
            return Json(new { val = false }, JsonRequestBehavior.AllowGet);
        }          
    }

有什么建議嗎?

通常,您無法使用Html.BeginForm()類的Ajax.BeginForm()上傳文件。 您必須使用JavaScript / jQuery提交表單元素。

解決方法如下:

$(document).ready(function(){
    $(document).on("submit", "#form1", function (event) {
            event.preventDefault();
            event.stopImmediatePropagation();
            var formData = new FormData(this);
            var url = this[0].action;

            $.ajax({
              url: url,
              type: 'POST',
              data: formData,
              success: function (response) {
                if (response) {
                  //do necessary work with response
                }
              },
              error: function() {
                //handle error here
              },
              cache: false,
              contentType: false,
              processData: false
            });
            return false;
          });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM