使用$ .getJSON(url)从视图中调用控制器操作方法

时间:2022-06-25 20:36:51

I have controller which accepts one argument from the view and it should return json data back to that same view.

我有控制器从视图中接受一个参数,它应该将json数据返回到同一个视图。

on controller action I have

我有控制器动作

public ActionResult GetDataById(int id)
{
    var data = ...give me data...
    return Json(data, JsonRequestBehavior.AllowGet);
}

On the view I have

在我看来

<ul id="#myList">
</ul>

(function () {
   var updateList = function (competitions) {
       $("#myList").html($("listTemplate").render(competitions))
                             .listview();
   };
   $(document).one("pageinit", function () {
        var url = "/Home/GetDataById/1";
        $.getJSON(url)
         .then(updateList);
    });    
} ());

<script id="listTemplate" type="text/x-jsrender">
   <li>
       <a href="{{:Name}}">{{:Id}}
           <span class="ui-li-count">{{:CountedData}}</span>
   </li>    
</script>

On debugging GetDataById is not hitted at all. When I put alert(url) before calling $.getJSON I'm getting correct url. What I'm doing wrong here?

在调试GetDataById时根本没有进入。当我在调用$ .getJSON之前放置alert(url)时,我得到了正确的url。我在这里做错了什么?

Update: Route config is

更新:路由配置是

routes.MapRoute(
       name: "Default",
       url: "{controller}/{action}/{id}",
       namespaces: new[] { "MyWebApp.Controllers" },
       defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

3 个解决方案

#1


1  

What would happen if you specify the Id as a parameter

如果将Id指定为参数,会发生什么

var url = "/Home/GetDataById";

$.getJSON(url, { id: 1 })

#2


1  

Just a wild guess, but are you running the site locally? If so is it in a virtual directory (i.e. do you go http://localhost/home/index or http://localhost/mysite/home/index)?

只是一个疯狂的猜测,但你是在本地运行该网站吗?如果是这样在虚拟目录中(即你是去http:// localhost / home / index还是http:// localhost / mysite / home / index)?

You could try this:

你可以试试这个:

var url = @Url.Content( "~/Home/GetDataById/1" );

#3


1  

I am using $.getJSON as described by Quite M. Porta's, but with a slight difference. I add a '/' to the end of the URL

我正在使用Quite M. Porta所描述的$ .getJSON,但略有不同。我在URL的末尾添加了一个'/'

$.getJSON("/Home/GetDataById/", { id: 1 }).then(updateList);

If all else fails, you could try that.

如果一切都失败了,你可以试试。

#1


1  

What would happen if you specify the Id as a parameter

如果将Id指定为参数,会发生什么

var url = "/Home/GetDataById";

$.getJSON(url, { id: 1 })

#2


1  

Just a wild guess, but are you running the site locally? If so is it in a virtual directory (i.e. do you go http://localhost/home/index or http://localhost/mysite/home/index)?

只是一个疯狂的猜测,但你是在本地运行该网站吗?如果是这样在虚拟目录中(即你是去http:// localhost / home / index还是http:// localhost / mysite / home / index)?

You could try this:

你可以试试这个:

var url = @Url.Content( "~/Home/GetDataById/1" );

#3


1  

I am using $.getJSON as described by Quite M. Porta's, but with a slight difference. I add a '/' to the end of the URL

我正在使用Quite M. Porta所描述的$ .getJSON,但略有不同。我在URL的末尾添加了一个'/'

$.getJSON("/Home/GetDataById/", { id: 1 }).then(updateList);

If all else fails, you could try that.

如果一切都失败了,你可以试试。