【问题标题】:display validation messages using JsonResult使用 JsonResult 显示验证消息
【发布时间】:2012-05-18 17:46:07
【问题描述】:

如何使用 JsonResult 和 AJAX 返回自定义验证消息?

这是我在 StudentDB 中添加学生的控制器操作。

更新:

    [HttpPost()]
    public ActionResult AddStudent(string studentName, int studentId)
    {
        var studentPresent = studentTable.Students.Where(s => s.StudentID == studentId&& b.StudentName == studentName);

        if (studentPresent == null || !studentPresent .Any())
        {
            var student = new Student()
                {
                    StudentName = studnetName,
                    StudentID = studentId,
                };
            studentTable.AddObject("Student", student);
            studentTable.SaveChanges();

         }
        return new JsonResult();      

    }

这是我的 JavaScript:

function addStudent() {
    $.ajax({
        type: 'POST',
        url: '/StudentAdmin/AddStudent',
        data: {
            studentName: $('#studentName').val(),
            studentNumber: GetTextBoxValue('#studentNumber'),            
        },
        success: function (result) {
            if ($('#studentPresent').val() == null) {
            showMessage('Success', 'Student saved successfully.', '', false);                
        } else {
            showMessage('Error', 'Student already present in database.', '', false);
        }
            GetGrid('#studentGrid').ajaxRequest();
            hideForm();
        },
        studentPresent: function (result) {
            showMessage('Error', 'Student Already present in Database.', '', true);
        }

    });
}

如果该学生已经存在于数据库中,我想显示“错误”消息。 另外,有没有办法向 JasonResult 传递更多验证消息?

提前致谢。

【问题讨论】:

标签: c# asp.net-mvc json jquery


【解决方案1】:

您可以将任何对象传递给JsonResult 对象,它将被序列化(或尝试序列化)到javascript。

return new JsonResult(new { Anything = "Hello World" });

这会产生一个像这样的 JSON 对象:

{"Anything":"Hello World"}

result 变量中呈现给您的javascript。

您上面的代码实际上没有显示任何正在生成的错误消息;如果要获取 SQL 异常的文本,则需要一个 try/catch 块。

编辑:

你会有这样的代码:

return Json(new { message = "Success" }); // success message
return Json(new { message = "Failure" }); // fail message

然后在javascript中,你的回调是这样的:

success: function(result)
{
     if(result.message == "Success")
       // Show success message
     else
       // Show Error Message
}

【讨论】:

  • 得到一个错误:System.Web.Mvc.JsonResult 不包含一个构造函数,当我尝试以下操作时采用 1 个参数:return new JsonResult(new { result = "Success" });跨度>
  • 你应该可以使用辅助方法:return Json(someObject);我很困惑。您只需在 JsonResult 上设置 Data 属性,而不是在构造函数中。
  • 如果我传递这样的对象:return new Json(new { result = "Success" });它仍然给我一个错误: System.Web.Mvc.Controller.Json is a method but is used like a type
  • 删除 new Json 并替换为 Json - 这是一种方法。
  • 是的,我做到了。现在没有错误。但是,它不会显示错误消息。即使条件不满足,它也会始终显示成功消息。
【解决方案2】:

你可以return Json(new {result = "Success"});

【讨论】:

  • 我在“Json”上收到以下错误:System.Web.Mvc.Controller.Json 是一种方法,但用作类型
  • 我已经编辑过了。只需在 Json 之前删除新的。请再次检查。
猜你喜欢
  • 2016-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多