【问题标题】:Using a Ternary in a @using Statement在 @using 语句中使用三元
【发布时间】:2015-03-10 12:36:50
【问题描述】:

全部,

我正在尝试使用@using 语句实现三元组,以便在我的三元组中定义的某些情况下切换@enctype。我收到一个 Razor 解析错误,指出我在 @using 语句中缺少一个结束“}”。这个错误具有误导性,因为我所有的花括号都匹配得很好。我将整个表单都包含在内,因为我看不到三元部分的语法有任何明显的问题。

看起来问题是 if/else 块,但不知道为什么......

任何想法为什么这是有问题的?更好的方法?

@using(Ajax.BeginForm("ProcessCompose", 
                      "ClubOpeningTool",
                      FormMethod.Post,
                      new AjaxOptions { UpdateTargetId = "update_panel" },
                      new { enctype = (fileAttachmentUnsupported ?  "application/x-www-form-urlencoded" : "multipart/form-data"), 
                      id = "ComposeForm" }))
{

    @Html.ValidationSummary(true)
     <fieldset style="width:90%">
         <p class="required"><strong>* Required</strong></p>
         <div id="update_panel"></div>
         <div>
             <span style="font-weight:bold; font-size:14px; color:#000000;">To:</span>
         </div>
         <div>
             <table id="compose" class="table-grid">
                 <tr>
                     <td><strong>Available @(isTeam? "Team Mambers": Model.Mode)</strong> <br />
                         @Html.ListBox("AvaliableRecipients", Model.AvailabeRecipients, new{@class="ncb-listbox"})
                     </td>
                     <td><button id="addAllRecipient" type="button">>></button><br /><button id="addRecipient" type="button">> </button><br /><button id="removeRecipient" type="button">< </button><br /><button id="removeAllRecipient" type="button"><<</button>
                         <script type="text/javascript">
                             $(function () {
                                 $('#addRecipient').click(function () {
                                     $('#AvaliableRecipients option:selected').appendTo('#Recipients');

                                 });
                                 $('#addAllRecipient').click(function () {
                                     $('#AvaliableRecipients option').appendTo('#Recipients');
                                     $("#Recipients option").attr("selected", "selected");

                                 });

                                 $('#removeRecipient').click(function () {
                                     $('#Recipients option:selected').appendTo('#AvaliableRecipients');
                                 });

                                 $('#removeAllRecipient').click(function () {
                                     $('#Recipients option').appendTo('#AvaliableRecipients');
                                 });

                             });

                             $("form").submit(function () {
                                 $('#Recipients').find("option").attr('selected', 'true');
                             });
                         </script>
                     </td>
                     <td><strong>Current Recipients</strong>&nbsp;<span class="required">*</span><br />
                         @Html.ListBox("Recipients", Model.Recipients,new{@class="ncb-listbox"})<br /><span class="messageBottom">@Html.ValidationMessageFor(model => model.Recipients)</span>
                     </td>
                 </tr>

             </table>

         </div>

         <div>
             @Html.LabelFor(model => model.Subject) &nbsp;<span class="required">*</span>
         </div>
         <div>
             @Html.EditorFor(model => model.Subject)<br />
             <span class="messageBottom">@Html.ValidationMessageFor(model => model.Subject)</span>
         </div>
        @if (!fileAttachmentUnsupported)
        {
            <div>
                <label>
                    File Attachment @Html.DisplayFor(model => model.FileName)
                </label> <i>(9MB file size limit)</i>

            </div>

            <input class="js-file-text" type="text" id="fileUploadFileName" name="fileUploadFileName">

            @Html.TextBoxFor(model => model.FileAttachment, new {type = "file", @class = "js-file-field"})
            <a class="btn blue js-file-btn">Browse</a>
        }
        else
        {
              <div>
                <label>
                    <b>Attachment feature not available for Internet Explorer 10 (IE10 or earlier)</b>
                </label> 
              </div>
        }


         @Html.HiddenFor(model => model.EmailID)
         @Html.HiddenFor(model =>model.NewClubId)
         @Html.HiddenFor(model =>model.Mode)
         @Html.HiddenFor(m => m.DraftOrSentViewMode)
         <div>
             @Html.LabelFor(model => model.Body)&nbsp;<span class="required">*</span>
         </div>
         <div>
             @Html.TextAreaFor(model => model.Body)<br />
             <span class="messageBottom">@Html.ValidationMessageFor(model => model.Body)</span>
         </div>


         <p>
             <input type="submit" id="btnComposeSend" name="Command" value="Send" class="btn blue saveSend"/>
             @if (! isTeam) {
                <input id="btnComposeSave" type="submit" name="Command" value="Save" class="btn blue saveSend"/>
             }
         </p>
     </fieldset>


}

【问题讨论】:

  • 这可能只是意味着您不需要@。在没有@ 的情况下尝试一下 - 特别是在@enctype 中;请参阅stackoverflow.com/a/12317656/23354 了解更多信息
  • 我至少建议重构代码......也许在某个地方的常规代码中使用方法? 370 个字符长的行不利于阅读。
  • 这里适合使用HTML helper;它可以让您将其重构为对该助手的调用。
  • 从 enctype 中删除 @ 没有任何效果,但这是个好主意

标签: c# asp.net-mvc-3 razor


【解决方案1】:

不知道为什么这不起作用,也许发布整个视图?话虽如此,如果是我,我会试试这个……

@using(Ajax.BeginForm(
        "ProcessCompose", 
        "ClubOpeningTool",
         FormMethod.Post,
         new AjaxOptions { UpdateTargetId = "update_panel" },
         new { @enctype = (fileAttachmentUnsupported ?  "application/x-www-form-urlencoded" : "multipart/form-data"), id = "ComposeForm" }))
{ ... }

【讨论】:

  • 非常感谢,但仍然遇到同样的解析错误,指出我的@using 缺少一个结束“}”我粘贴了整个表单以防万一。
【解决方案2】:

问题是由 if/else 块中未关闭的输入标签引起的。不知道为什么智能感知没有发现这一点,或者为什么错误消息似乎与实际问题无关。谢谢大家

之前

 <input class="js-file-text" type="text" id="fileUploadFileName" name="fileUploadFileName">

之后

 <input class="js-file-text" type="text" id="fileUploadFileName" name="fileUploadFileName" />

【讨论】:

    猜你喜欢
    • 2021-12-24
    • 2010-10-11
    • 2018-04-04
    • 2013-09-06
    • 2011-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-05
    相关资源
    最近更新 更多