You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VB.net的MVC共用编辑新增页面中隐藏新增页自增Id字段

问题根源

你当前的代码写死了可见的UserId表单组,无论新建/编辑模式都会渲染,因此新增页面也会显示Id字段。@Html.HiddenFor仅会生成隐藏的input域,不会影响你显式编写的可见表单组的显示逻辑。

解决方案

通过判断UserId的默认值区分新建/编辑模式,仅在编辑模式下渲染可见的Id表单块即可:

@* 隐藏域始终保留,用于表单提交时传值 *@
@Html.HiddenFor(Function(Model) Model.UserId)

@* 只有UserId不为默认值(编辑模式)时才渲染可见的Id表单 *@
@If Model.UserId <> 0 Then
    @<div class="form-group">
        @Html.LabelFor(Function(model) model.UserId, htmlAttributes:=New With {.class = "control-label col-md-2"})
        <div class="col-md-10">
            @Html.EditorFor(Function(model) model.UserId, New With {.htmlAttributes = New With {.class = "form-control"}})
            @Html.ValidationMessageFor(Function(model) model.UserId, "", New With {.class = "text-danger"})
        </div>
    </div>
End If

如果你的UserId是可空整数类型(Integer?),将判断条件改为Model.UserId IsNot Nothing即可。

内容的提问来源于stack exchange,提问作者Yogesh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.21 02:27:04