如何调用从JavaScript在MVC3一个控制器的方法? [英] How to call a Controller method from javascript in MVC3?

查看:351
本文介绍了如何调用从JavaScript在MVC3一个控制器的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯使用MVC3架构,C#.NET。我需要时立即比较文本框的内容(用户ID)与数据库焦点更改到下一个字段即密码字段。所以我想用onblur事件的用户ID字段,然后调用控制器方法。可以在任何告诉我如何处理这个问题?由于我是个新手,code片段是高度AP preciated。

Im using MVC3 architecture, c#.net. I need to compare text box content(User ID) with the database immediately when focus changes to the next field i.e., Password field. So I thought to use onblur event to the User Id field which then calls the Controller method. Can any tell me how to approach to this problem? As Im a newbie, code snippets are highly appreciated.

在此先感谢,

Prashanth

Prashanth

推荐答案

下面是一个例子。 你的控制器方法的实施

Here is an example. Example of your Controller Method

[HttpPost] // can be HttpGet
public ActionResult Test(string id)
{
     bool isValid = yourcheckmethod(); //.. check
     var obj = new {
          valid = isValid
     };
     return Json(obj);
}

和这将是你的JavaScript功能。

and this would be your javascript function.

function checkValidId(checkId)
{
    $.ajax({
         url: 'controllerName/Test',
         type: 'POST',
         contentType: 'application/json;',
         data: JSON.stringify({ id: checkId }),
         success: function (valid)
         {
              if(valid) { 
                  //show that id is valid 
              } else { 
                  //show that id is not valid 
              }
         }
    });
}

这篇关于如何调用从JavaScript在MVC3一个控制器的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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