0

当模型值为空/空时,我想在剃刀视图中设置文本框的默认值。这就是我设置默认值的方式,但它目前正在覆盖模型的值,即使它存在:

<div class="form-group">
    @Html.LabelFor(Function(model) model.PostAction, htmlAttributes:=New With {.class = "control-label col-md-2"})
    <div class="col-md-10">
        @Html.EditorFor(Function(model) model.PostAction, New With {.htmlAttributes = New With { .Value = "Active", .class = "form-control"}})
        @Html.ValidationMessageFor(Function(model) model.PostAction, "", New With {.class = "text-danger"})
    </div>
</div>

我希望默认值是一个可选值,仅在模型值为 null 时才设置

4

1 回答 1

0

我想通了:

    <div class="col-md-10">
        @If String.IsNullOrEmpty(Model.PostAction) Then
            @Html.EditorFor(Function(model) model.PostAction, New With {.htmlAttributes = New With {.Value = "Active", .class = "form-control"}})
        Else
            @Html.EditorFor(Function(model) model.PostAction, New With {.htmlAttributes = New With {.class = "form-control"}})
        End If
        @Html.ValidationMessageFor(Function(model) model.PostAction, "", New With {.class = "text-danger"})
    </div>
于 2021-04-18T03:58:00.650 回答