如何在TextBox(ASP.NET MVC)前面添加美元符号

时间:2019-10-15 12:18:34

标签: .net asp.net-mvc

如何根据此代码在“费率”文本框的前面添加美元符号?我想要一个美元符号,这样人们就不需要自己键入“ $”。这是我的代码:

<div class="row" style="margin-bottom: 20px">
    <div class="col-md-3">
        @Html.Label("lblP1", "Rate 1", htmlAttributes: new { @class = "control-label", @style = "margin-right: 30px" })
        <br />
        @Html.EditorFor(m => m.Pricing.Price1, null, new { htmlAttributes = new { @type = "string", @class = "form-control" } })
        <br />
        @Html.ValidationMessageFor(m => m.Pricing.Price1, "", new { @class = "text-danger" })
    </div>
    <div class="col-md-3">
        @Html.Label("lblExp1", "Expiry 1", htmlAttributes: new { @class = "control-label", @style = "margin-right: 30px" })
        <br />
        @Html.EditorFor(m => m.Pricing.Exp1, "{0:yyyy-MM-dd}", new { htmlAttributes = new { @type = "text", @class = "form-control datepicker1", @id = "datepicker1", @placeholder = "MM/DD/YYYY" } })
    </div>
</div>

它看起来像:

enter image description here

3 个答案:

答案 0 :(得分:1)

您可以使用DisplayFormat属性来装饰模型属性:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
public decimal Price1 { get; set; }

然后在您看来:

@Html.EditorFor(m => m.Pricing.Price1)

您可以使用DisplayFormat对到期的DateTime应用类似的格式。


或者您可以使用EditorFor而不是TextBoxFor来传递格式字符串:

@Html.TextBoxFor(m => m.Pricing.Price1, "{0:c}")

答案 1 :(得分:0)

您似乎已经在使用Bootstrap。您可以使用Input group在输入前面绘制一个美元符号,如下所示:

 public function up()
{

    Schema::create('favorites', function (Blueprint $table) {
        $table->Increments('id');
        $table->bigInteger('user_id')->unsigned();
        $table->bigInteger('product_id')->unsigned();
        $table->timestamps();

    });

    Schema::create('favorites', function (Blueprint $table){

        $table->bigInteger('user_id')->unsigned()->index();
        $table->foreign('user_id')->references('id')->on('users');

        $table->bigInteger('product_id')->unsigned()->index();
        $table->foreign('product_id')->references('id')->on('products');
    });

}

public function down()
{
    Schema::dropIfExists('favorites');
}

答案 2 :(得分:0)

使用Bootrap 4和Fontawesome

  <div class="form-group">
                    @Html.LabelFor(model => model.Pricing.Price1, htmlAttributes: new { @class = "control-label font-weight-bold" })
                    <div>
                        <div class="input-group">
                            <div class="input-group-prepend">
                                <span class="input-group-text" id="basic-addon1"><i class="fas fa-dollar-sign"></i></span>
                            </div>
                           @Html.TextBoxFor(m => m.Pricing.Price1, "{0:c}")
                            @Html.ValidationMessageFor(model => model.Pricing.Price1, "", new { @class = "text-danger" })
                        </div>
                    </div>
                </div>