【问题标题】:Alert to show from controller从控制器显示警报
【发布时间】:2013-02-14 12:23:56
【问题描述】:

我正在开发一个用户管理应用程序,我需要在数据库中添加一个用户。

When click of a button in client screen ( index.cshtml ), a JQuery dialog will popup and fill the user details ( Adduser.cshtml (partial class) ) . On click of Save button the data will pass to controller validate it and call the model method to add the user. If the user already exists the webservice will return error message. In controller the exception will be catched and wants to show it to user.

出现异常后,我需要将错误消息显示为警报或将其写入 AddUser.cshtml。以下是我尝试过的各种选项以及与之相关的问题

  1. return Partial("AddUser", model ) - 这里的问题是 Adduser.cshtml 在单独的页面中显示,而不是在客户端屏幕中显示为对话框。

  2. 将异常消息存储在 TemData 中,返回到 RedirectToAction("index") 并尝试在页面加载时加载 Adduser.cshtml - 这里的问题是 JQuery 对话框变为空白。

  3. return Json { new success = true } - 问题是消息显示的是单独的页面,并且没有给出确切的错误消息。

  4. return content(alert(errmessage)) - 正在显示警报消息,但单击确定按钮时会显示一个空白的单独页面。

  5. 尝试在 Adduser.cshtml 中使用 Javascript 调用模型方法 - 不确定调用模型方法来添加用户。如果成功,如何从 Adduser.cshtml 重定向到客户端屏幕

请任何人帮助我。

    [HttpPost]
    public ActionResult AddClient(ClientModel mCust, string command)
    {
        var clientObj = new Metadata.Client.Service.Client();
        ClientModel clientModel = new ClientModel();

        if (command == "Save")
        {

            if (!ModelState.IsValid)
            {
                return PartialView(mCust);
            }


            clientObj.ClientType = new Metadata.Client.Service.ClientType();
            clientObj.ClientName = mCust.Client.ClientName;
            clientObj.ClientCode = mCust.Client.ClientCode;
            if (mCust.ClientTypeSelectId != 0)
                clientObj.ClientType.ClientTypeId = (mCust.ClientTypeSelectId) - 1;
            else
                clientObj.ClientType.ClientTypeId = mCust.ClientTypeSelectId;

            try
            {
                clientObj = clientModel.AddNewClient(clientObj);
            }
            catch (Exception ex)
            {
                //TempData["addclient"] = null;
                //TempData["addclient"] = ex.Message;
                //mCust.SetClientTypeList();
                //mCust.WebResponse.Message = ex.Message;
                //return PartialView(mCust);

                // we're gonna show this in a ValidationSummary
                ModelState.AddModelError("", ex.Message);
                return PartialView("AddClient", mCust);

            }
        }

        return RedirectToAction("Index");

    }

【问题讨论】:

    标签: jquery json jquery-ui asp.net-mvc-4


    【解决方案1】:

    这不是您应该寻找的行动变化。基本上你应该使用 ajax 和回调方法发布表单,你可以检查“成功”或“错误”并从那里显示对话框。

    您必须在 ajax 完成后进行一些回调,例如:

    <script type='text/javascript'>
    function onSuccess(result){if(result.get_data()=='error'){alert('error'); }}
    </script>
    

    要进一步扩展它,您可以从控制器发送消息,例如“error|error_message”。这样,您可以通过将 result.get_data() 拆分为字符“|”来检查它是成功还是错误并显示您传递的消息。

    【讨论】:

    • 谢谢 Gaurav 我会试试这个并告诉你。
    • 是的。我以前做过。如果您在执行此操作时遇到任何问题,请告诉我。
    • 当我将返回指定为以下行时无法应用 |运算符键入字符串和字符串。你能告诉我需要发送回消息的格式吗 return Json(new { "error"|ex.Message }, JsonRequestBehavior.AllowGet);
    • 为什么不尝试返回 ContentResult? Json 解析也是一种开销。尝试返回新的 ContentResult{content="error|"+ex.Message};
    • Gaurav 绝对运行良好。非常感谢您更快、更及时的回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 2020-10-02
    • 2020-12-05
    • 2017-01-06
    • 2011-09-21
    相关资源
    最近更新 更多