2
@using (Html.BeginForm())
{
    int controlWidth=250;
    @Html.TextBoxFor(m => m.studentFirstName, new { style = "width:@(controlWidth)px;" })

this code renders as

            <input data-val="true" data-val-required="The Student First Name field is required." id="studentFirstName" name="studentFirstName" style="width:@(controlWidth)px;" type="text" value="" />

understandably, I want it to render as

            <input data-val="true" data-val-required="The Student First Name field is required." id="studentFirstName" name="studentFirstName" style="width:250px;" type="text" value="" />

I've seen posts like ASP.NET MVC Razor Concatenation that suggest this approach is right. Any ideas what I'm doing wrong?

Community
  • 1
  • 1
Dave Alperovich
  • 32,320
  • 8
  • 79
  • 101

1 Answers1

5

I don't think you can do that. Inline templates can only be used inside HTML like <li style="@(controlWidth)"> or if your method supports inline template. See http://vibrantcode.com/blog/2010/8/2/inside-razor-part-3-templates.html/ on how to write a method that supports inline template.

Zeeshan Ali
  • 347
  • 3
  • 12
  • I'm beginning to see. Are you saying razor variables work in some areas but not in style parameters? – Dave Alperovich Aug 30 '12 at 18:55
  • Yes it can work with methods that support inline template i.e. a method takes `Func template` as argument & calls it just like what the author is doing in above post. But I don't see this is possible with C# properties and ofcourse style is property here. It is commonly used as html encoded expression like `@model.Message`. – Zeeshan Ali Aug 30 '12 at 19:16
  • zeeshan, what about this post http://stackoverflow.com/questions/4702957/asp-net-mvc3-razor-concatenation ? I can't believe all the posters are lying that
  • worked for them. The answer has a +42
  • – Dave Alperovich Aug 30 '12 at 19:24
  • 2
    Here `id` is not .NET property but attribute of `
  • ` tag
  • – Zeeshan Ali Aug 30 '12 at 19:45