Html.Editorfor value Default

Asked

Viewed 721 times

3

How could you put Default value on a Html.EditorFor ?

Understanding my code,

I have a field that’s a filter when I click on EditorFor open the calendar, I’d like the datetime.now but only visual without performing the filter.

Because if I put it in the model (get;set) I can show more already by performing the filter.

Code used.

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

I’ve tried it like this.

@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 a default value?

  • I don’t quite understand it, it’s something how that that you want?

2 answers

2

EditorFor won’t work because EditorFor need to intuit the type to generate the field. For precaution, removed the possibility of the EditorFor receive value because this would be an endless source of bugs.

Now, the following construction works:

@Html.TextBoxFor(model => model.ModelPaging.Filter.dataInicioIndicadores, new { @Value = ViewBag.DataAtual })
  • Cigado,! Thanks for the answer, I had already tried with textbox but it did not appear the calendar. I followed what Igor said and solved the problem.!

  • What do you mean, "there was no calendar"?

  • When I use Html.Editorfor and its property is of the Datetime type it shows a Jquery calendar,

  • jQuery UI datepicker? This has nothing to do with what you asked. By the way, it’s pretty simple to set up datepicker for both EditorFor how much for TextBoxFor.

  • Yes, because if I opened Jquery and try to locate Editorfor it does not find, because I tried to do this before everything. I’m beginner did not go too deep in Jquery just translate the calendar to EN, if you can put where I can go to configure I thank you, you do not need to put the whole code only where it is more or less.

  • In fact, in new browsers there are some features like: <input type="date"/> when Editorfor identifies a date it puts type="date" and the browser itself shows a Datepicker, with no need for plugins or anything like that. If the browser is old it shows a common textbox.

  • Following example: http://www.w3schools.com/html/tryit.asp?filename=tryhtml_input_date

Show 2 more comments

1


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

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 to the controller:

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

and if the dateIndicators are not Nullable (Datetime?)

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

Sincerely yours truly, Igor Quirino

  • Igor.! Thanks, Assigns in controller and did exactly what you wanted!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.