javascript - 使用 @using Html.BeginForm asp.net MVC 防止提交时重新加载页面

标签 javascript jquery asp.net ajax asp.net-mvc

我有一个包含模型的页面,我想将其与附加文件一起发送到 Controller (文件不在模型中)。到目前为止,我提交的一切都正常,但由于我只是部分完成,所以我想将模型和文件发送到 Controller 并保持在同一页面上。如果上传成功,我还想获得对页面的响应,以便我可以处理表单元素。这就是我所拥有的:

查看

    @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>
    }

Controller

// 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) 发送表单,我的文件始终为空。感谢任何帮助

最佳答案

Controller 代码:

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/

相关文章:

javascript - jQuery 自动完成多个值

javascript - JQuery 在不同的 IE8 安全设置下表现异常

javascript - 如何迭代jquery选择器的结果

c# - 模拟的 HttpRequest 丢失了 QueryString

javascript - 为什么下面的测试没有通过?

javascript - 以字符串作为第一个操作数的加法运算符可以返回非字符串吗?

javascript - 如何使用 Angular js 过滤数组

javascript - 当DIV低于某个特定值时自动设置DIV的高度

asp.net - 使用目标 _blank 进行跨页面发布会使源页面无法运行

asp.net - RadioButton 控件控件下的文字换行