This question already has answers here:
How to append whole set of model to formdata and obtain it in MVC
                            
                                (3个答案)
                            
                    
                4年前关闭。
        

    

我有一个带有模型的页面,我想与其他文件一起发送到控制器(文件不在模型中)。到目前为止,我提交的一切都很好,但是由于我要部分完成,所以我想将模型和文件发送到控制器,并保留在同一页面上。如果上传成功,我也想获得对页面的响应,这样我就可以处理表单元素。这就是我所拥有的:

视图

    @model Test.Controllers.HomeController.MyClass
    @{
        ViewBag.Title = "Index";
    }

    @using (Html.BeginForm("Save", "Home", FormMethod.Post, new { role = "form", enctype = "multipart/form-data" }))
    {
        @Html.AntiForgeryToken()
        @Html.TextBoxFor(m=>m.Number)

        <input id="file" name="file" type="file" multiple>

        <button class="btn btn-primary center-block" id="saveButton">
            Save <span class="glyphicon glyphicon-ok" style="color: white;" type="submit"></span>
        </button>
    }


控制者

// GET: Home
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public virtual JsonResult Save (MyClass model, List<HttpPostedFileBase> file)
    {
        return Json(true);
    }

    public class MyClass
    {
        public int Number { get; set; }
    }


保存完成后,我想获得响应(Json或其他),以便重新加载一些数据网格等。如果我尝试使用ajax(form.serialize)发送表单,则我的文件始终为空。任何帮助表示赞赏

最佳答案

控制器代码:

public class EditorController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public virtual JsonResult Save(MyClass model)
    {
        var fileName = Request.Files[0].FileName;

        using (var memoryStream = new MemoryStream())
        {
            Request.Files[0].InputStream.CopyTo(memoryStream);
            var fileContent = memoryStream.ToArray();
        }
        return Json(true);
    }

}


班级代码:

    namespace _12_12_2015.Models
{
    public class MyClass
    {
        public int Number { get; set; }
    }
}


查看代码:

    @using _12_12_2015.Models
@model MyClass
@{
    ViewBag.Title = "Index";
}
@using (Html.BeginForm("Save", "Editor", FormMethod.Post, new { role = "form", enctype = "multipart/form-data"}))
{
    @Html.TextBoxFor(m => m.Number)
    <input id="file" name="file" type="file" multiple>
    <button class="btn btn-primary center-block" id="saveButton">
        Save <span class="glyphicon glyphicon-ok" style="color: white;" type="submit"></span>
    </button>
}
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
    $('form').submit(function (ev) {
        ev.preventDefault();
        var data = new FormData();
        var fileInput = $('#file')[0];
        var file = fileInput.files[0];
        data.append(file.name, file);
        var number = $("#Number").val();
        data.append("Number", number);
        $.ajax({
            url: '@Url.Action("Save", "Editor")',
            type: 'POST',
            data:  data,
            processData: false,
            contentType: false,
            success: function() {
                alert("bhasya")
            }
        });
    });
</script>

关于javascript - 防止使用@using Html.BeginForm asp.net MVC在提交时重新加载页面,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34246729/

10-12 12:57
查看更多