MVC 3剃须刀调用服务器端函数使用jQuery [英] mvc 3 razor call a server side function using jquery

查看:187
本文介绍了MVC 3剃须刀调用服务器端函数使用jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个code到从服务器获取的局部视图OT把它放在一个div

i'm using this code to get a partial view from the server ot put it in a div

    $.ajax(
    {
        type: "POST",
        url: "MyControler/MyAction",
        success: function (result) {
            $('#partialView').html(result);      
        },
        error: function (req, status, error) {
            alert("Coudn't load partial view");
        }
    });

现在我想做同样的事情,但我会调用一个简单的返回梅索德一个字符串,并把结果在一个文本框

now i would like to do the same thing but i'd call a simple methode returning a string and put the result in a textbox

    $.ajax(
    {
        type: "POST",
        url: "MyControler/MyMethod",
        success: function (result) {
            $('#myTextBox').html(result);      
        },
        error: function (req, status, error) {
            alert("Coudn't load partial view");
        }
    });

在梅索德会像

    public string MyMethod()
    {
        returning "hello";
    }

这显然不工作,但有什么办法,使工作?在那里我应该为梅索德使用属性

obviously it doesn't work but is there a way to make it work ?? is there an attribute i should use for the methode

在此先感谢

PS:

这是我冲在第一个答案我想这一点,但它不能正常工作

from what i red in the first answer i tried this but it doesn't work

    public ActionResult Hello()
    {
        return Content("Hi there!");
    }

是有什么错Ajax调用??

is there something wrong with the ajax call ??

$.ajax(
    {
        type: "POST",
         url: "MyControler/Hello",
        success: function (result) {
            $('#myTextBox').html(result);      
     },
    error: function (req, status, error) {
        alert("Coudn't load partial view");
    }
});

PS

我想尝试在一个干净的项目,它使用简单的方法返回一个字符串正常工作

I wanted to try on a clean project and it worked fine using a simple method returning a string

,因为它的工作我试图把它落实到我的项目,但现在看来,这只是从在Global.asax文件中设置任何知道我应该做的,使其在每个网页的工作起始页的作品?

since it worked i tried to implement it to my project but it seems it only works from the starting page set up in the global.asax file any idea what i should do to make it work in every pages ???

推荐答案

您正在一个 HttpPost 拨打。因此,请确保您的操作方法都装饰有 HttpPost 属性。还总是试图用 Url.Action HTML辅助方法的路径操作方法。

You are making an HttpPost call. So make sure your Action method is decorated with HttpPost attribute. Also always try to use Url.Action HTML Helper method for the path to the action method.

下面code应该工作,假设你有您好 Action方法$ P $在 MMyControlerController

The below code should work, assuming you have the Hello Action method present in MMyControlerController.

$.ajax(
        {
         type: "POST",
         url: "@Url.Action("Hello","MyControler")",
         success: function (result) {
             alert("result from server "+result);
             $('#myTextBox').html(result);      
         },
        error: function (req, status, error) {
            alert("Coudn't load partial view");
        }
});

您的操作方法

[HttpPost]
public ActionResult Hello()
{
  return Content("This is String Content");
}

这篇关于MVC 3剃须刀调用服务器端函数使用jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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