Don’t use Data- attribute with anonymous object in C#

Hi Friends,

Today, i just like to share one small tip. Never use Data- attribute with C# anonymous object as C# compiler doesn’t interpret this. Rather than this you can use data_ attribute, which will be understood by the compiler and then it will be converted as data- once page finally rendered. Below, I have small example for the same.

@Html.RadioButtonFor(model => model.IntentToExport, “true”, new { id = “exportYes”, name = Model.ShippingExportFormFieldName, data_action = @Url.Action(“GetCountryAndProductUsageDropDown”, “Shipping”), _checked = Model.IntentToExport.IsTrue() ? “selected” : “” })

will emit

<input id=”exportYes” checked=”checked” name=”ExportCompliance.IntentToExport” type=”radio” value=”true” data-action=”/cart/us/en/rc1005388/Shipping/GetCountryAndProductUsageDropDown” data-val=”true” data-val-required=”Please choose Trade Compliance” />

If you try data- in anonymous object itself, C# compiler will throw error.

Happy Coding