An example of using JQuery with ASP.NET/MVC with Rick Stahl''s Web Utility.
Let's assume you want to use JQuery where you wish to have client side data sent back to a controller without a full page post back.
For the project:
Download the utility and add references for Westwind.Utilities and Westwind.Web to your project. You'll also need to add ScriptVariableInjectionHelper.cs to your helpers folder.
ScriptVariableInjectionHelper provides an easy way for server code to publish strings into client script code. This object basically provides a mechanism for adding key value pairs and embedding those values into an object that is hosted on the client.
For the Master Page:
asp:contentplaceholder id="Scripts" runat="server">
For the Controller:
Now that we have a way to inject our script and a target to inject it into, we need to simply send it down for manipulation.
From your ActionMethod we add a call to a new InjectClientScript method...
public virtual ActionResult Edit(Guid id)
Criterion model = _CriterionSvc.GetCriterion(id);
private void InjectClientScript(object model)
ScriptVariables scriptVars = new ScriptVariables("view");
// add our ProfileCommon data (user context)
// add our model being injected
if (model != null)
// call the westwind utility and inject into viewdata
ViewData["clientScript"] = scriptVars.GetClientScript(true);
For the view:
The output is injected into a variable called view, which in turn contains our Controller objects we injected (Profile and Model). Note Model contains objects of its own. In this example, CriterionType is one such object. You can configure the westwind utility to traverse child objects or not.
$.PopulateDropdown("CriterionTypeId", "/CriterionType/GetCriterionTypeCollection/" + view.Model.CriterionType.DomainId, view.Model.CriterionTypeId);