Archive of articles classified as' "Programming"

Back home

Resharper & Entity Framework Generated Code


Been looking for a way to get Resharper to ignore the code that is generated by Entity Framework for a while. Sure, I can ignore the entire project, but since we also had some repo/Unit Of Work implementations in the same project, that’s really not an option.

The final solution was to edit the code generating T4 (.tt) templates of Entity Framework to wrap the code in a custom region, i.e. ”EF Generated code”.

Then tell Resharper to simply skip checking anything that is within region ”EF Generated Code” from ReSharper -> Options -> Generated Code


No Comments

MVC3 Ajax partial view validation


Ran into a wall of trouble after getting Ajax to work properly in my previous post. Turns out that validation rules are not applied to partial views loaded with Ajax (Even when using the provided Ajax.ActionLink helper).

Thanks to this excellent blogpost i managed to work it out. But not until after bashing my head against the wall until numb.

The main problem was not so much the actual validation scripts, it was pretty clear to me that the validation rules would have to be rebuilt after injecting new objects into the DOM, but the fact that MVC refused to acually render the data-val / data-val-required properties for the elements that needed validating.

These properties are usually rendered by Html.EditorFor / ValidationMessageFor when used as in the example above.

This is how it should look:

<input class="text-box single-line" 
       data-val-required="This field is required!" 

But when including a partial view through Ajax inside this outer form, the ability to use BeginForm inside that partial view is lost due to the fact that we can not have nested forms. And without BeginForm, no data-val properties 🙁

The key to getting the partial view to render the properties was to wrap the contents like the following code snippet:

@using(new MvcForm(ViewContext))
    <div class="editor-label">
        @Html.LabelFor(model => model.Property)
    <div class="editor-field">
        @Html.EditorFor(model => model.Property)
        @Html.ValidationMessageFor(model => model.Property)


And then to get the validation going, call xhalent’s brilliant js-function after appending the results of the Ajax request to the DOM.

  data: {},
  success: function (data) {             

1 Comment

Simple and painless Ajax in MVC3


Even though Microsoft really came through with MVC3 and added a lot of sweetness, I still find myself reluctant to use the provided Ajax-functionality.

Here’s why:

This approach is nice and simple, yes. But what if I don’t like having global javascript functions all over the place to handle the results of the action. It kind of pains me to see that I can use ”unobtrusive” validation functionality in MVC3 but not unobtrusive Ajax handling.

This is solved by implementing the ”ajaxification” of a normal Html.ActionLink in a separate javascript,
such as proposed here

I needed a more reusable way of doing this, but portable enough to be painlessly lifted to other projects and easy to use for other developers in the project. So i slapped together this jQuery plugin…

Using it is quite straightforward, or at least it seems to me at the time of writing this.

First, we create the controller action that we want to call with the Ajax link:

This action returns a view, so a view will have to be created as well. A simple plain text partial view will do fine. Be sure not to load any layout for this though. The action could have returned JSON as well,
as both cases can be handled by the jQuery plugin.

With a controller action and a view in place, we create the ActionLink in the view where we need the Ajax functionality:

There we go!, now we can declare all our events and handlers well outside the view layout as demonstrated in the following gist:

No Comments