views:

227

answers:

3

I currently have a web form aspx page that calls RegisterClientScriptBlock. This sends down message text that I use for client side validation e.g.

    <script type="text/javascript">
    //<![CDATA[
    var c_errorMessages = {
        RequiredField : "* Mandatory field"
    };
    //]]>
    </script>

The values are generated on the server side based on culture and resource files. I believe you cannot use RegisterClientScriptBlock with MVC. Any ideas on how I can achieve this with MVC?

A: 

The validation that comes with ASP.NET MVC 2 has built-in support for localized validation messages when using client-side validation. This will take care of registering the client scripts.

Having said that, the most common way to do this in ASP.NET MVC would probably be to simply have the controller feed the view with the data required to render the client script. An example of this would be

Action method in controller:

ViewData["MessagesToClient"] = new[] { "Abandon", "All", "Hope" };
return View("MyView");

Some code in MyView.aspx:

<%= RenderPartial("MyPartialView") %>

Some code in MyPartialView.ascx:

<script>
<% foreach (var message in (IEnumerable<string>)ViewData["MessagesToClient"]) { %>
  alert("<%= message %>");
<% } %>
</script>

This way, MyView.aspx doesn't necessarily need to know about the data MyPartialView.ascx needs.

bzlm
Thanks for reply bzlm. The javascript I will be sending down will also contain flags that I require for non validation reasons e.g.var c_merchant = { CaptureBillingAddress : false };So I still need to register this block of javascript on the view
Noel
@Noel Why not just put the flags and things in the Model for the View, and put the code to render the script block in a strongly-typed User Control which renders the JavaScript?
bzlm
Sounds like it will work. Do you know of an example where a user control is used to render javascript?
Noel
@Noel I've updated my answer to include an example. In your case, you might want to send a list of `CMerchant` instead of `string`.
bzlm
A: 

Thanks for answer bzlm. My solution is pretty much the same as yours except I am not using a user control In my controller I add to viewdata(this would be generated from method):

ViewData["javascriptBlockRequired"] =
    "\r\n<script type='text/javascript'>\r\n//<![CDATA[\r\nvar c_merchant = { \r\n\tSomeField: false};\r\nvar c_errorMessages = {\r\n\tRequiredField : '* Required' \r\n};\r\n//]]>\r\n</script>\r\n";

In my view I output the script at the start of the body:

<body>
<%= ViewData["javascriptBlockRequired"] %>
....
<body/>
Noel
+1  A: 

Create an extension method for HtmlHelper which takes your model and renders the javascript.

If you are doing validation:

Have a look at Castle Validation attributes. http://castleproject.org. You can add attributes to your model (class) ...

[ValidateNonEmpty]
public string Name { get; set; }

and your extension method can determine the presence of these and write the appropriate javascript / jquery.

public static string JSValidation<T>(this HtmlHelper<T> html, T model) where T : class {}

And in your view use the helper:

<%= Html.JSValidation(Model) %>

ASP.NET MVC in Action has a brilliant example of this in Chapter 13.

CRice