当前位置: 动力学知识库 > 问答 > 编程问答 >

Asp.net mvc pass parameter to controller from view

问题描述:

I new to asp.net mvc technology.

Here is my Controller:

public ActionResult SessionData()

{

return View();

}

When button pressed on the view I need to implement ajax call and pass two strings to the controller above.

My question is how make ajax calls in asp.net mvc?Can I use HTML helper to generate ajax function?

网友答案:

You could use jQuery.ajax:

<script>
    $.ajax({
        url: '@Url.Action("MyAction")',
        type: 'POST',
        data: {
            arg1: 'value1',
            arg2: 'value2'
        },
        success: function(result) {
            alert('Successfully passed data to controller');
        }
    });
</script>

and the corresponding controller action may look like this:

public ActionResult MyAction(string arg1, string arg2)
{
    ... do something with those arguments and return a result

    return Json(new
    {
        someProperty = "some value"
    });
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: