Title: How to make razor view engine treat "[" and "]" as literals and not index
Tags: asp.net-mvc;razor;razorengine
Question: I have a strong need to use custom input naming rules like:
<input type="text" value="1" name="products[123][qty]"&gt;
Can't use @HtmlTextBoxFor()
I decided to move "products" and "qty" text literals to be constants. Have a strong need for it too.
Then I tried:
<input id="qty" type="text" value="1" name="@OrderContext.PRODUCTS_KEY[123][@OrderContext.QUANTITY_KEY]"&gt;
And I get an error because "[" and "]" are used in Razor like index separators, I need to use it like string literals. I tried to escape them with "\" but then I get "\[" and "\]" in HTML instead of desired "[" and "]".
How to escape square brackets properly? What can you suggest?
The real need is to move input name keys like "products" and "qty" to be defined and later changed in one place.

Here is the accepted answer: <input id="qty" type="text" value="1" name="@(OrderContext.PRODUCTS_KEY)[123][@OrderContext.QUANTITY_KEY]"&gt;
Parentheses right after @ limits the scope of the parsing as code to just the content within the parentheses.
Comment for this answer: Great! And even shorter than ``

Here is another answer: You could argue that what your doing is data related. So it belongs in the model. So edit your model thus:
public class OrderContext
public string PRODUCTS_KEY {get; set;}
public int QUANTITY_KEY {get; set;}
public string PRODUCTS_KEY_InputName
get
return string.format("{0}[123][{1}]", PRODUCTS_KEY, QUANTITY_KEY);
then you can simply print it in your view:
<input id="qty" type="text" value="1" name="@PRODUCTS_KEY_InputName"&gt;
Comment for this answer: Thanks, as for now dicided to go with ``
Comment for this answer: And even shorter with @Lasse Espeholt solution

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:01/31/14', 'FavoriteCount': 2.0, 'PostTypeId': 1, 'Tags': '<asp.net-mvc><razor><razorengine>', 'Title': 'How to make razor view engine treat "[" and "]" as literals and not index', 'ViewCount': 288, 'id': 21477952, 'user_AboutMe': '', 'user_AccountId': 40886.0, 'user_DisplayName': 'Zelid', 'user_Reputation': 6415.0, 'user_Views': 276.0, 'user_id': 118657.0}}