开发者

calling a custom @helper method in ms web pages from a client side event

开发者 https://www.devze.com 2023-03-11 18:18 出处:网络
I have a myPage.cshtml page. i have written a @helper method( myMethod() ) in myHelper.cshtml. I can call the helper method inline in the page (开发者_运维问答 @myHelper.myMethod(); ) and it works jus

I have a myPage.cshtml page. i have written a @helper method( myMethod() ) in myHelper.cshtml. I can call the helper method inline in the page (开发者_运维问答 @myHelper.myMethod(); ) and it works just fine.

How do i call this same method from a user initiated event like ( menu.item.click, button.click, link click ) ?


Because your method executes in server-side code, you'll have to create a way to call the code on your server from the client. For example, you could have an action method like this:

[HttpPost]
public ActionResult MyHelperCaller()
{
    // Returns the contents of the 'myHelperCaller' view:
    return this.View();
}

...where the myHelperCaller View contains a call to your myMethod() helper method.

Then to call that from the client you'd use something like:

<div id="myHelperTarget"></div>
<input id="myHelperTrigger" type="button" value="Call MyHelper" />
<script language="text/javascript">

$("#myHelperTrigger").click(function () {
    // Loads the myHelperCaller view into the myHelperTarget div:
    $("#myHelperTarget").load("/MyController/MyHelperCaller");
});

</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消