Monday, April 16, 2012

How to call javascript function from a controller in MVC3

I have looked round and I cannot find a solutions hence I find myself here. from what i have read i would been able to do this in asp.net web forms using RegisterClientScript or RegisterClientScriptBlock. I cannot find this in any MVC3 documentation.
I have the following function in a MVC 3 View :



MyTest View :





<div data-role="content">

<div id="mappingTable"></div>

</div>

</section>
@section Scripts {
<script type="text/javascript">

$("#jqm-home").live('pageinit', function () {
addTableRow(' adding data to table <br/>');
});

//I want to the able to call the function from the controller.
function addTableRow(msg) {
$('#mappingTable').append(msg);
};

</script>
}


In My Controller I have the following.



public class MyTestController : Controller
{
#region Class Constractor

public MyTestController()
{
Common.ControllerResourceEvent += new System.EventHandler<ResourceEventArgs>(Common_ResourceEvent);
}

private void Common_ResourceEvent(object sender, ResourceEventArgs e)
{
//I want to call the function addTableRow and pass ResourceEventArgs
}

No comments:

Post a Comment