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

时间:2018-07-15 12:37:49

标签: c# asp.net asp.net-mvc

在视图上:

<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我尝试了几种方法,但不知道为什么它为空

 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);
        }          
    }

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

通常,您无法使用let buildingDescription = NSEntityDescription.entity(forEntityName: "Buildings", in: context)! let building1 = Buildings(entity: buildingDescription, insertInto: context) let building2 = Buildings(entity: buildingDescription, insertInto: context) let building3 = Buildings(entity: buildingDescription, insertInto: context) let groupOfBuildingDescription = NSEntityDescription.entity(forEntityName: "GroupOfBuildings", in: context)! let groupOfBuildings1 = GroupOfBuildings(entity: groupOfBuildingDescription, insertInto: context) groupOfBuildings1.mainBuilding = building1 groupOfBuildings1.groupOfBuildings = Set([building2, building3]) building1.groupOfBuildings = groupOfBuildings1 building2.groupOfBuildings = groupOfBuildings1 之类的Ajax.BeginForm()上传文件。您必须使用JavaScript / jQuery提交表单元素。

这是解决方案:

Html.BeginForm()