如何清除编辑器对于使用脚手架创建视图时创建的输入元素。

时间:2018-08-20 03:59:29

标签: c# asp.net-mvc razor

我已经创建了一个像这样的视图模型:

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>

我认识到的是,当我访问页面/视图时,默认情况下html input字段会用诸如0的值填充ViewMoel中的int,decimal,datetime等属性。如何确定默认情况下所有input字段都为空?

1 个答案:

答案 0 :(得分:0)

使用可空类型,例如int?,decimal?和DateTime?。实例化对象时,每个属性将被初始化为默认的数据类型。整数默认为0,而整数?默认为null。等等。您仍然可以在属性上放置[Required],以确保用户在将其发布回服务器之前将其输入。