Asp.net MVC JsonResult更迭消息 [英] Asp.net MVC JsonResult succes Message

查看:153
本文介绍了Asp.net MVC JsonResult更迭消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的控制器:

    public JsonResult Success() { return Json(new { Success = true, Message = "Data Added Succefully" }); }
    public JsonResult Error(string message) { return Json(new { Success = false, Message = message }); }

    [HttpPost]
    public JsonResult CreateAjax(TAUX taux)
    {
        if (ModelState.IsValid)
        {
            try
            {
                foreach (short i in taux.SelectItems)
                {
                    taux.CAT_ID = i;
                    db.TAUX.Add(taux);
                    db.SaveChanges();

                }
                return Success();
            }
            catch (Exception err)
            {
                return Error(err.Message);
            }
        }

        ViewBag.CAT_ID = new SelectList(db.CATEGORIE, "CAT_ID", "LIBELLE", taux.CAT_ID);
        ViewBag.C_GARANT = new SelectList(db.GARANTIE, "C_GARANT", "LIB_ABREGE", taux.C_GARANT);

        return Error("The server wasn't able to do something right now.");
    }

这是我的PartialView CreateAjax

This is My PartialView CreateAjax:

@model pfebs0.Models.TAUX
....
@using (Html.BeginForm("CreateAjax", "Taux", FormMethod.Post, new { id = "form" }))
{...}

这是我的看法首页

@model IEnumerable<pfebs0.Models.TAUX>
...
     <script>
         $.ajax({
             url: "/",
             method: "POST",
             data: getMyData(),

             success: function (json) {
                 if (json.Success) {
                     alert("Wow, everything was fine!");
                 } else {
                     alert(json.Message);
                 }
             },

             // This will be trigered whenever your ajax call fails (broken ISP link, etc).
             error: function () {
                 alert("Something went wrong. Maybe the server is offline?");
             }
         });

</script> 
...
       @Html.ActionLink("Ajouter", "Create", "Taux",
                new { id = "btnAdd", @class="btn btn-default"})
</p>
...
    @section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
    $(function () {

        $.ajaxSetup({ cache: false });

        $('#btnAdd').click(function () {
            $('.modal-dialog').load(this.href, function () {
                $('#modalDiv').modal({
                    backdrop: 'static',
                    keyboard: true
                }, 'show');
            });
            return false;
        });
    });
....
 </script> }

我想要做的,是Insertionn后表现出成功的警报,但插入后,我重定向到新的页面本地主机/ Taux / ajaxCreate 它告诉我此消息 {成功:真正的消息:新增的数据Succefully},而不是显示在首页弹出成功的消息 页。什么是错在这里?

What I'm trying to do here is to show success alert after Insertionn but after Insertion I'm redirected to new Page Localhost/Taux/ajaxCreate where It show me this message {"Success":true,"Message":"Data Added Succefully"} instead of showing PopUp with success message in Index Page. What's wrong here ?

推荐答案

您应该使用

@using (Ajax.BeginForm(....))
{   }

与此时,相应的参数。

with the appropiate parameters.

请参阅<一href="http://stackoverflow.com/questions/20746784/how-can-i-return-a-json-result-to-a-ajax-beginform">How我可以返回一个JSON结果到Ajax.BeginForm 了解详细信息。

有可能是用脚本的一些问题,以及。 什么是你想要做的:

There might be some issues with the script as well. What are you trying to do with:

<script>
     $.ajax({
         url: "/",
         method: "POST",
         data: getMyData(),

更新 好吧,这应该工作:

UPDATE Ok, this should work:

1)使用原来的

@using (Html.BeginForm

2)把Ajax调用的函数:

2) put the ajax call in a function:

<script type="text/javascript">
    function postData()
    {
         $.ajax({
             url: '@Url.Action("CreateAjax", "Taux")',
             method: "POST",
             data: $('#form').serialize(),

            ....
    }

3)修改 TYPE =提交的提交按​​钮,并添加TYPE =按钮

onclick="postData()"

属性。

4)变更AJAX网址:

4) change ajax url:

url: '@Url.Action("CreateAjax", "Taux")',

5)加改变getMyData功能功能

5) add the change the getMyData function function

data: $('#form').serialize(), 

这篇关于Asp.net MVC JsonResult更迭消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆