控制器获得NULL值?

时间:2010-05-25 22:49:16

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

我正在尝试通过jQuery $ .post调用控制器,但是我的控制器方法的参数不断获得NULL值,尽管它似乎设置类似于其他控制器方法。

CONTROLLER

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SearchWeatherLocations(string searchFor)
{
    //Do Some Magic
}

Global.asax中

routes.MapRoute("SearchWeatherLocations", "Home/SearchWeatherLocations/{searchFor}",
new
{
    controller = "Home",
    action = "SearchWeatherLocations"
});

jQuery Call From View

<script type="text/javascript" language="javascript">
    $(document).ready(function () {
        GetWeatherLocations("seat");
    });
    function GetWeatherLocations(sSearchFor) {
        var divToBeWorkedOn = '#locations';
        var webMethod = '<%= Url.Action("SearchWeatherLocations", "Home") %>/';
        var url = webMethod + sSearchFor;
        $.post(url, function (data) {
            $('#locations').children().remove();
            for (var count in data) {
                $('#locations').append("<li>" + data[count].LocationName + "&nbsp;(" + data[count].LocationCode + ")</li>");
            }
        });
    }
</script>

1 个答案:

答案 0 :(得分:1)

试试这样的帖子;

的jQuery

$.post("/Articles/jQueryAddComment", { commentText: commentText, id: id, type: commentType }, function(newCommentListHTML) 
{
});

控制器

public ActionResult jQueryAddComment(string commentText, int id, string type)
{
}
相关问题