c# - 如何清除使用脚手架创建视图时创建的 EditorFor 输入元素。
问题描述
我创建了一个这样的视图模型:
public class M_Master
{
public string SUPWH { get; set; }
public string STKGL { get; set; }
public string SALGL { get; set; }
public string COSGL { get; set; }
public decimal ROQ { get; set; }
public int MIN { get; set; }
public int MAX { get; set; }
public int LT { get; set; }
public string PRODSPEC { get; set; }
public string REMK1 { get; set; }
public string REMK2 { get; set; }
public decimal openingQty { get; set; }
public decimal openingAmt { get; set; }
public decimal onHandQty { get; set; }
public decimal ytdReceiptQty { get; set; }
}
然后我使用这个 ViewModel 来创建一个 View。所以视图看起来像下面这样:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>INV_Master</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.PROD, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PROD, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PROD, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.WH, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.WH, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.WH, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DESCR, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DESCR, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DESCR, "", new { @class = "text-danger" })
</div>
</div>
我意识到的是,当我访问页面/视图时,input
默认情况下,html 字段会填充为 ViewMoel 中的那些属性的值,例如 0,这些属性是 int、decimal、datetime。如何确保input
默认情况下所有字段为空?
解决方案
使用可空类型,如 int?、decimal? 和 DateTime?。当对象被实例化时,每个属性都会被初始化为默认的数据类型。int 默认为 0,而 int? 默认为空。等等。您仍然可以在属性上放置 [Required] 以确保它们在被回发到服务器之前由用户输入。
推荐阅读
- swift - 如何通过 Graph API 获取用户的性别或电子邮件等特定字段
- swift - 使用不同的数据库提供程序测试 Vapor 3
- makefile - 尝试在 Windows Subsystem for Linux 中编译内核模块错误
- batch-file - SetupComplete.cmd 与正常运行时不同
- react-native - 为什么 `activeTintColor` 在我的应用程序中不起作用?
- haskell - Haskell 类型错误 - 无法将预期类型“a”与实际类型“RE a”匹配
- apache-camel - 如何同步执行不同交易所的骆驼路线?
- angularjs - Angular 1x:仅过滤来自 2 列的数据
- c# - 从 Npgsql.EntityFrameworkCore.PostgreSQL 2.0.0 升级到 2.1.2 外键问题
- arrays - 尝试在 Laravel 5.6 中获取非对象的属性时出错