1

After upgrading to ReSharper 8, I'm starting to see a bunch of warnings in my JavaScript that shouldn't be there.

In a Razor .cshtml page, I have:

@Html.HiddenFor(model => model.Id)
@Html.EditorFor(model => model.Name)

And in the JavaScript/jQuery, I have:

var id = $('#Id').val();
var name = $('#Name').val();

In the new ReSharper, it looks like they added an inspection rule that searches for IDs matching the CSS query. However, it isn't able to infer IDs from the MVC/Razor syntax, so I am getting warnings on nearly every JQuery selector.

Is there any way to get ReSharper to infer IDs from the MVC/Razor syntax? I'm aware that I could just turn off the inspection rule, but that's less desirable.

tereško
  • 58,060
  • 25
  • 98
  • 150
chkimes
  • 1,127
  • 13
  • 20
  • I'd advise separating your javascript out into separate js files. Not only would this issue be cleared up, you get alot of other benefits along with it - minimizing, cdn deliver, file caching ect. – asawyer Jul 26 '13 at 13:09
  • 1
    I can confirm that having the script in a separate .js file does NOT clear this up. ReSharper is smart enough to find the corresponding view, but still doesn't realize that the element id will be generated by the HtmlHelper. – Rich Sep 12 '13 at 20:17

1 Answers1

0

Usually I hate people who don't answer a question directly, but offer a "why don't you ... instead" solution. Yet here I go... ;-) Maybe you still like the solution.

Because of refactoring, I recently managed to break javascript code (mostly jquery selectors) very similar to yours.

Through adding the following extensions to the HtmlHelper (I didn't come up with them, found them on the web)

public static string GetFullHtmlFieldName<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression) {
    return helper.ViewData.TemplateInfo.GetFullHtmlFieldName(expression);
}

public static string GetFullHtmlFieldId<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression) {
    return helper.ViewData.TemplateInfo.GetFullHtmlFieldId(expression);
}

public static string GetFullHtmlFieldName<TModel, TProperty>(this TemplateInfo templateInfo, Expression<Func<TModel, TProperty>> expression) {
    return templateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));
}

public static string GetFullHtmlFieldId<TModel, TProperty>(this TemplateInfo templateInfo, Expression<Func<TModel, TProperty>> expression) {
    return templateInfo.GetFullHtmlFieldId(ExpressionHelper.GetExpressionText(expression));
}

I was able to make my selectors "model-type-safe":

var id = $('#@Html.GetFullHtmlFieldId(model => model.Id)').val();

While this isn't a direct solution for your problem, it will satisfy ReSharper, and you get the extra benefit of "model-type-safety" The only downside is that it makes your JavaScript code a bit uglier - personally I prefer that over things breaking silently.

UweB
  • 4,080
  • 2
  • 16
  • 28
  • 1
    The code supplied is not valid - that is, you cannot assign 'expression' as a string which is the param type of GetFullHtmlField* I believe you want to use: ExpressionHelper.GetExpressionText(expression) in the place of just expression. – Todd Carter Aug 15 '13 at 13:05
  • +1, sorry for the late follow-up. You are correct, and the reason it's running fine here is that I've got two more extension methods which do the conversion like you described. I've added them to my original posting. – UweB Aug 22 '13 at 07:41