c# – 为什么Visual Studio 2012不能格式化这个有效的Razor代码

所以我在一个构建和运行良好的视图中有一些有效的Razor语法代码.但是,在Visual Studio 2012中,代码将无法正确格式化,Razor在解析此文档时似乎“脆弱”,当我自动格式化代码时,CTRL K D.某些内容浮动,丑陋,不格式化为嵌套的父子结构体.我无法在我的第二个if条件中添加@来修复浮动左侧
HTML内容.

@foreach (var customField in Model.CustomFields)
{
<tr>
    <td>
        @customField.label
    </td>
    <td>
        <div class="col-md-4 padding-0">
            @switch (customField.custom_field_type_id)
            {
                case Constants.CustomFieldType.checkbox:
                    foreach (var opt in customField.custom_field_options.Select((x, i) => new { Data = x, Index = i }))
                    {
                        var optValue = customField.custom_field_values.FirstOrDefault(item => item.custom_field_option_id == opt.Data.custom_field_option_id && item.custom_field_id == customField.custom_field_id);

                <div class="checkbox">
                    <label>@opt.Data.custom_field_option_value</label>

                    @if (optValue != null)
                    {
                        var optName = customField.custom_field_id + "_" + opt.Data.custom_field_option_id + "_" + optValue.custom_field_value_id;
                        <input type="checkbox" id="CustomFieldEdit_@opt.Data.custom_field_option_id" name="CustomFieldEdit_@optName" @(optValue.value.ToLower() == "true" ? "checked" : "") />
                        <input type="hidden" id="CustomFieldOrig_@opt.Data.custom_field_option_id" name="CustomFieldOrig_@optName" value="@optValue.value" />
                    }
                    else
                    {
                        var optName = customField.custom_field_id + "_" + opt.Data.custom_field_option_id + "_0";
                        <input type="checkbox" id="CustomFieldEdit_@(opt.Data.custom_field_option_id)" name="CustomFieldEdit_@optName" @(opt.Data.is_checked ? "checked" : "") />
                    }
                </div>
                    }

                    break;

                         case Constants.CustomFieldType.text_input:
                    var textinputVal = customField.custom_field_values.FirstOrDefault();
                    if (textinputVal != null)
                    {
                        var textInputName = customField.custom_field_id + "_0_" + textinputVal.custom_field_value_id;
                <input type="text" id="CustomFieldEdit_@(customField.custom_field_id)" name="CustomFieldEdit_@(textInputName)" value="@textinputVal.value" class="form-control" />
                <input type="hidden" id="CustomFieldOrig_@(customField.custom_field_id)" name="CustomFieldOrig_@(textInputName)" value="@textinputVal.value" class="form-control" />
                    }
                    else
                    {
                        var textInputName = customField.custom_field_id + "_0_0";
                <input type="text" id="CustomFieldEdit_@(customField.custom_field_id)" name="CustomFieldEdit_@(textInputName)" class="form-control" />
                    }
                    break;

            }
        </div>
    </td>
</tr>

}

查看Visual Studio 2012 Professional如何格式化我的视图代码的屏幕截图

最佳答案 看起来你很好地使用括号来逃避值,所以Razor不会将它们解析为电子邮件地址,但看起来有些转义需要它们的圆括号.试试这个:

@if (optValue != null)
{
    <input type="checkbox" id="CustomFieldEdit_@(opt.Data.custom_field_option_id)" name="CustomFieldEdit_@(customField.custom_field_id)_@(opt.Data.custom_field_option_id)_@(optValue.custom_field_value_id)" @(optValue.value.ToLower() == "true" ? "checked" : "")>
    <input type="hidden" id="CustomFieldOrig_@(opt.Data.custom_field_option_id)" name="CustomFieldOrig_@(customField.custom_field_id)_@(opt.Data.custom_field_option_id)_@(optValue.custom_field_value_id)" value="@optValue.value">
}
else
{

    <input type="checkbox" id="CustomFieldEdit_@(opt.Data.custom_field_option_id)" name="CustomFieldEdit_@(customField.custom_field_id)_@(opt.Data.custom_field_option_id)_0" @(opt.Data.is_checked ? "checked" : "")>
}

更新以解决更新的问题

尝试这样的事情.

<div class="checkbox">
    <label>@opt.Data.custom_field_option_value</label>


    @if (optValue != null)
    {
        var optName = customField.custom_field_id + "_" + opt.Data.custom_field_option_id + "_" + optValue.custom_field_value_id;
        <input type="checkbox" id="CustomFieldEdit_@opt.Data.custom_field_option_id" name="CustomFieldEdit_@(optName)" @(optValue.value.ToLower() == "true" ? "checked" : "") />
        <input type="hidden" id="CustomFieldOrig_@opt.Data.custom_field_option_id" name="CustomFieldOrig_@(optName)" value="@(optValue.value)" />
    }
    else
    {
        var optName = customField.custom_field_id + "_" + opt.Data.custom_field_option_id + "_0";
        <input type="checkbox" id="CustomFieldEdit_@(opt.Data.custom_field_option_id)" name="CustomFieldEdit_@(optName)" @(opt.Data.is_checked ? "checked" : "") />
    }

</div>

编辑第三个问题更新

每次不断出现的主要问题是Razor错误地将代码的某些部分误认为是合法格式的电子邮件地址,但对于人类而言,它们并非如此.确保任何具有紧跟在纯HTML /文本之后的@的任何内容都括在括号中.

做:

CustomFieldEdit _ @(customField.custom_field_id)

CustomFieldEdit_@customField.custom_field_id

(@ customField.custom_field_id本身就没问题了.)

点赞