上传图片 - 模式弹出窗口MVC不关闭

时间:2016-04-15 13:05:42

标签: javascript asp.net-mvc serialization modal-dialog

我有问题。我打开我的网站,例如:localhost:9999 / Product / Index

单击“创建”图标,显示带有页面/产品/包装的模式弹出窗口

点击“保存”。

数据库中的所有记录都正确,但它将我重定向到page / Product / Crate,我只想关闭我的模态窗口。

我做了什么sholuld?

型号:

public partial class Prod
{
    public string ProductName { get; set; }
    public string ProductDescription { get; set; }
    public byte[] PFile { get; set; }
}

索引视图:

  

@using(Html.BeginForm(" Create"," Products",FormMethod.Post,new {   enctype =" multipart / form-data" })){

                       @ Html.ActionLink("","创建","产品",null,新{data_modal ="",id =& #34; btnCreate",@ class =" btn btn-small   btn-primary pull-right fa fa-cart-plus" })

     

@ Html.ActionLink(""," Create"," Products",null,new {id =   " btnCreate",@ class =" btn btn-small btn-primary pull-right fa   FA-车加" })

}

创建视图:

@using (Html.BeginForm("Create", "Products", FormMethod.Post, new { id = "form1", enctype = "multipart/form-data" }))
        {
            @Html.AntiForgeryToken()

            <div class="modal-body">

                <div class="form-horizontal">
                    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

                    <div class="form-group">
                        @Html.LabelFor(model => model.ProductName, "Name", htmlAttributes: new { @class = "control-label col-md-3" })
                        <div class="col-md-9">
                            @Html.EditorFor(model => model.ProductName)
                            @Html.ValidationMessageFor(model => model.ProductName, "", new { @class = "text-danger" })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.ProductDescription, "Name", htmlAttributes: new { @class = "control-label col-md-3" })
                        <div class="col-md-9">
                            @Html.EditorFor(model => model.ProductDescription)
                            @Html.ValidationMessageFor(model => model.ProductDescription, "", new { @class = "text-danger" })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.PFile, "File", htmlAttributes: new { @class = "control-label col-md-3" })
                        <div class="col-md-9">
                            @Html.TextBoxFor(model => model.PFile, new { type = "file", name = "imageF" })
                            @Html.ValidationMessageFor(model => model.PFile, "", new { @class = "text-danger" })
                        </div>
                    </div>

                </div>

            </div>

            <div class="modal-footer">
                <button class="btn" data-dismiss="modal">Anuluj</button>
                <input class="btn btn-primary" type="submit" value="Zapisz" />
            </div>
        }

控制器:

[HttpPost]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Exclude = "PFile")] Prod pro, HttpPostedFileBase imageF)
        {                    
            if (ModelState.IsValid)
            {

                    if (imageF != null)
                    {
                        pro.PFile= new byte[imageF.ContentLength];
                        imageF.InputStream.Read(pro.PFile, 0, imageF.ContentLength);
                    }


                     db.Prods.Add(pro);
                     db.SaveChanges();
                     return Json(new { success = true });

            }

            return PartialView("Create", pro);
        }

modalform.js

  

$(function(){

$.ajaxSetup({ cache: false });

$("a[data-modal]").on("click", function (e) {

    // hide dropdown if any
    $(e.target).closest('.btn-group').children('.dropdown-toggle').dropdown('toggle');


    $('#myModalContent').load(this.href, function () {


        $('#myModal').modal({
            /*backdrop: 'static',*/
            keyboard: true
        }, 'show');

        bindForm(this);
    });

    return false;
});
     

});

     

function bindForm(dialog){

$('form1', dialog).submit(function () {
    $.ajax({
        url: this.action,
        type: this.method,
        data: $('#file').serialize(),
        contentType: 'multipart/form-data',
        success: function (result) {
            if (result.success) {
                $('#myModal').modal('hide');
                //Refresh
                location.reload();
            } else {
                $('#myModalContent').html(result);
                bindForm();
            }
        }
    });
    return false;
}); }

请帮助解决这个问题。

0 个答案:

没有答案
相关问题