Dave Alperovich
Dave Alperovich

Reputation: 32500

Razor variable in string not rendering properly

@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?

Upvotes: 2

Views: 5400

Answers (1)

Zeeshan Ali
Zeeshan Ali

Reputation: 347

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.

Upvotes: 5

Related Questions