简体   繁体   English

使用Ajax调用在部分视图上提交

[英]Partial View on Submit using Ajax call

Here is my jquery code: 这是我的jQuery代码:

<script type="text/javascript">
    $("#submitfileform").submit(function () {
        $.ajax({

            type: 'POST',
            contentType: 'application/html;charset=utf-8',
            dataType:'html',
            success:function (result) {
                $('#tablepartialview').html(result);
            },
            error:function (xhr, status) {
                alert(status);
            }
        })


        });
</script>

and here is html.beginform, 这是html.beginform,

     @using (Html.BeginForm("PropertyColumnMap", "ImportFile", FormMethod.Post, new { enctype = "multipart/form-data", @class = "form single-col",id="submitfileform"}))
{
                    <input type="file" name="uploadFile" id="uploadFile" value=""/>
                    <select id="assetlist" name="assetlist">
                          <option>...</option></select>

                   <input class="btn btn-primary" type="submit" value="Submit" id="submitfile"/>
}
<div id="tablepartialview">


</div>

What happens is, on submit, I get the partial view of the same page 'Index' in div-'tablepartialview', instead of another page 'PropertyColumnMap', which I want. 发生的事情是,在提交时,我在div-tablepartialview中得到了同一页面“索引”的局部视图,而不是我想要的另一页面“ PropertyColumnMap”。 After the ajax call is done,it redirects to action 'PropertyColumnMap', and then I get the view for PropertyColumnMap. Ajax调用完成后,它将重定向到操作“ PropertyColumnMap”,然后获得PropertyColumnMap的视图。

public ActionResult PropertyColumnMap(FormCollection f, HttpPostedFileBase uploadFile)
        {

            String fileid = Import(uploadFile);



                var excel = new ExcelQueryFactory(Session[fileid].ToString());
                excel.DatabaseEngine = DatabaseEngine.Ace;
                var workSheetName = excel.GetWorksheetNames().Last();
                var assetname = f["assetlist"].ToString();
                Mapping(assetname, workSheetName, fileid);


            return PartialView("PropertyColumnMap");


        }

If its possible please include following js to your project 如果可能,请在您的项目中包含以下js

http://malsup.github.com/jquery.form.js http://malsup.github.com/jquery.form.js

Then you can use 那你可以用

$("#submitfileform").ajaxSubmit({
        type: 'POST',
        success:function (result) {
            $('#tablepartialview').html(result);
        },
        error:function (xhr, status) {
            alert(status);
        }
           });

As you are using MVC, just switch your Html.BeginForm to use the Ajaxified Ajax.BeginForm instead. 使用MVC时,只需将Html.BeginForm切换为使用Ajaxified Ajax.BeginForm

It allows for many options including specifying the id of the target element to update (eg 'tablepartialview'). 它允许许多选项,包括指定要更新的目标元素的ID(例如“ tablepartialview”)。

eg 例如

@using (Ajax.BeginForm("PropertyColumnMap", "ImportFile", new AjaxOptions(){ HttpMethod = "POST", UpdateTargetId = "tablepartialview"}, new { enctype = "multipart/form-data", @class = "form single-col", id = "submitfileform" }))
{
    <input type="file" name="uploadFile" id="uploadFile" value="" />
    <select id="assetlist" name="assetlist">
        <option>...</option>
    </select>

    <input class="btn btn-primary" type="submit" value="Submit" id="submitfile" />
}
<div id="tablepartialview">
</div>

You probably have to install the Ajax unobtrusive NuGet package to provide the wiring, but it is quite simple and does not require you to write any extra JQuery for the view. 您可能必须安装Ajax兼容的NuGet软件包以提供连接,但是它非常简单,不需要为视图编写任何额外的JQuery。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM