Domanda

I have a View-model with a few properties that are int and when I use an @Html.EditorFor in the View a text box is generated but it fills it with the number 0. I don't want the text box to be prefilled with anything, how can I stop this without making the property nullable?

ViewModel:

public class TransactionVM
{
    [DisplayName("Ticket #"), DataType(DataType.Text)]
    public int TicketNumber { get; set; }

    [DisplayName("Customer")]
    public IEnumerable<SelectListItem> Customers { get; set; }
    public int CustomerId { get; set; }

    [DisplayName("Carrier")]
    public IEnumerable<SelectListItem> Carriers { get; set; }
    public int CarrierId { get; set; }

    [DisplayName("Driver")]
    public IEnumerable<SelectListItem> Drivers { get; set; }
    public int DriverID { get; set; }

    [DisplayName("Product")]
    public IEnumerable<SelectListItem> Products { get; set; }
    public int ProductId { get; set; }

    [DisplayName("Gross Gallons\n(Max: 6,000)")]
    public decimal GrossGallons { get; set; }

    [DisplayName("Net Gallons")]
    public decimal NetGallons { get; set; }

    [DisplayName("Sale Start Number")]
    public string SaleStartNumber { get; set; }

    [DisplayName("Transaction Date and Time")]
    public DateTime TransactionDate { get; set; }
}

Controller:

        TransactionVM model = new TransactionVM();
        return View(model);

        using (var db = new DynamicDbContext())
        {
            model.Customers = new SelectList(db.GetList<Customer>(c => c.Id > 0).ToList(), "Id", "Name");
            return View(model);
        }

View:

    <tr>
        <td>
            @Html.LabelFor(m => m.TicketNumber)
        </td>
        <td>
            @Html.EditorFor(m => m.TicketNumber)
        </td>
    </tr>

This is not about disabling model binding. I want to place data in the view to populate the dropdowns and also need all the data bound to it on post to manipulate it in the controller.

È stato utile?

Soluzione

I'm not sure and didn't try it but you can try to set value property like this:

@Html.EditorFor(m => m.TicketNumber, new { @value = "" })

Edit: another way would be using jQuery:

<script>
    $(function() {
        $("#TicketNumber").val("");
    });
</script>

Altri suggerimenti

Add [DefaultValue(null)] DataAnnotaion to your model properties. Or at least set default values in controller action (one that uses [HttpGet]) to prepare page! Or remove value property via jQuery in client side.

An alternative to setting the model property to Nullable is editing the ModelState directly.

public ActionResult Create()
{
  var model = new TransactionVM();
  ModelState.SetModelValue("TicketNumber", new ValueProviderResult("", "", System.Globalization.CultureInfo.CurrentCulture));
  return model;
}

You have to be careful to do this in the POST action as well, and then only in cases where the value is 0.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top