I have the following code...
@using (Html.BeginForm())
{
LocationQuantityPick Qty.
@foreach (FullInventory inv in Model)
{
@Html.DisplayFor(m => inv.InventoryLocationName)@Html.DisplayFor(m => inv.Quantity)@Html.EditorFor(m => inv.Quantity)}
Pick
}
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.
Talk1:
I can see that the use case is to show current quantity and eventually change it in the text box, but wouldn't it be better to just show the editor box with the existing value? leaving it like it is will leave the quantity unchanged, chaning it will change it...
Solutions1
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('');
Solutions2
Try doing the following:
@Html.EditorFor(m => inv.Quantity, new { @Value = "" })
Talk1:
I do not think this tactic works if you already have a value. Anyway, either way, it means you end up with 2 "value" attributes. One of them capitalised. It may be a dirty fix when you need to do something quickly but it is not recommended. It may be excessive but I blank them with jquery.
Solutions3
Make Quantity a nullable type in your model, e.g.
int? Quantity { get; set;}
Talk1:
That is going to change the database model though. It seems overkill to make a form work correctly.
Solutions4
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.