代码之家  ›  专栏  ›  技术社区  ›  johnny 5

在标记内输出原始数据

  •  0
  • johnny 5  · 技术社区  · 7 年前

    我正在尝试使用MVC输出一个可选属性。我在支付选项上有一个for循环,我正在尝试设置默认选择的选项,如果这是选择的选项。

    var selected = "";
    var optionLabel = $"{ senderAccount.AccountName } / {senderAccount.Balance } {senderAccount.Currency}";
    if (senderAccount.AccountId == Model.AccountId)
    {
        selected = "selected=\"selected\"";
    }
    <option data-to="@receiverAccount.Address" value="@(senderAccount.AccountId)" @Html.Raw(selected)>
        @optionLabel
    </option>
    

    当我转到我的视图时,我看不到所选的字符串,尽管它已被设置。我试过:

    @Html.Raw(selected)
    @(selected)
    @selected
    

    这些选项似乎都不起作用。我不想重复选项定义。如何在MVC中将原始字符串作为可选属性输出

    1 回复  |  直到 7 年前
        1
  •  0
  •   johnny 5    7 年前

    这解决了我的问题,但我仍然想知道如何发布原始html。在3之后的MVC版本中,如果您发布的值为null,则该属性将可选地显示

    string selected = null;
    var optionLabel = $"{ senderAccount.AccountName } / {senderAccount.Balance } {senderAccount.Currency}";
    if (senderAccount.AccountId == Model.AccountId)
    {
        selected = "selected";
    }
    <option data-to="@receiverAccount.Address" value="@(senderAccount.AccountId)" selected="@selected">
        @optionLabel
    </option>