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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I'm beginning to see. Are you saying razor variables work in some areas but not in style parameters?
Yes it can work with methods that support inline template i.e. a method takes Func<int, object> 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 <span>@model.Message</span>.
zeeshan, what about this post stackoverflow.com/questions/4702957/… ? I can't believe all the posters are lying that <li id="item_@(item.TheItemId)"> worked for them. The answer has a +42
Here id is not .NET property but attribute of <li> tag

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.