acbarberi
acbarberi

Reputation: 79

MVC4/Razor - Need to set editor box to be empty

I have the following code...

@using (Html.BeginForm())
{
<table class="tableList">
    <thead>
        <tr>
            <td>Location</td>
            <td>Quantity</td>
            <td>Pick Qty.</td>
        </tr>
    </thead>
    <tbody>
        @foreach (FullInventory inv in Model)
        {
            <tr>
                <td>@Html.DisplayFor(m => inv.InventoryLocationName)</td>
                <td>@Html.DisplayFor(m => inv.Quantity)</td>
                <td>@Html.EditorFor(m => inv.Quantity)</td>
            </tr>
        }
    </tbody>
</table>

<div class="midButton">
    <button type="submit" id="btnPick" value="Pick">Pick</button>
</div>
}

I would like the "EditorFor" Quantity to be an empty box. It automatically puts the Quantity value in the Quantity box as I would expect. However, I want the Quantity text box to be empty by default when the page loads.

Upvotes: 1

Views: 1764

Answers (4)

AthibaN
AthibaN

Reputation: 2087

If your model holds some value, clear them in the controller before returning the View using

ModelState.Clear();

To make sure the browser doesn't populate the previous values try this,

@Html.EditorFor(model => model.MyField,new { autocomplete = "off" }))

Let me know if it helps ! Cheers.

Upvotes: 0

SkyBlues87
SkyBlues87

Reputation: 1235

You could either set the model.Quantity to be empty in your controller before you render the view or do it via jquery on the page once the page loads:

$('.tableList tbody .Quantity').val('');

Upvotes: 1

Paddyd
Paddyd

Reputation: 1870

Try doing the following:

@Html.EditorFor(m => inv.Quantity, new { @Value = "" })

Upvotes: 1

juju
juju

Reputation: 449

Make Quantity a nullable type in your model, e.g.

int? Quantity { get; set;}

Upvotes: 0

Related Questions