Html.EditorFor default value

3

How could I put Default value in a Html.EditorFor ?

Understanding my code,

I have a field that is a filter when I click on EditorFor opens the calendar, I would like the datetime.now to appear but only visual without doing the filter.

If I put in the model (get; set) I can show more by doing the filter.

Code used.

@Html.EditorFor(model => model.ModelPaging.Filter.dataInicioIndicadores)

I've tried it that way.

@Html.EditorFor(model => model.ModelPaging.Filter.dataInicioIndicadores, new { htmlAttributes = new { @Value = ViewBag.DataAtual} })

@Html.EditorFor(model => model.ModelPaging.Filter.dataInicioIndicadores, new { @Value = ViewBag.DataAtual})

Does anyone know a way to pass the default value?

    
asked by anonymous 23.11.2015 / 15:35

2 answers

1

You can change the model.ModelPaging.Filter.dataInicioIndicadores property to:

private DateTime? _dataInicioIndicadores;
public DateTime dataInicioIndicadores
{
    get
    {
        if(_dataInicioIndicadores.HasValue)
            return _dataInicioIndicadores.Value;
        else
            return DateTime.Now;
    }
    set
    {
        _dataInicioIndicadores = value;
    }
}

or simply assign the value in the controller:

if(!model.ModelPaging.Filter.dataInicioIndicadores.HasValue)
{
    model.ModelPaging.Filter.dataInicioIndicadores = DateTime.Now
}

and if the StartDateId is not Nullable (DateTime?)

if(!model.ModelPaging.Filter.dataInicioIndicadores == DateTime.MinValue)
{
    model.ModelPaging.Filter.dataInicioIndicadores = DateTime.Now
}

Sincerely, Igor Quirino

    
23.11.2015 / 16:27
2

EditorFor will not work because EditorFor needs to guess the type to generate the field. As a precaution, they removed the possibility of EditorFor receiving value because that would be an infinite source of bugs.

Now the following construction works:

@Html.TextBoxFor(model => model.ModelPaging.Filter.dataInicioIndicadores, new { @Value = ViewBag.DataAtual })
    
23.11.2015 / 16:09