MVC3 HTMLHelper defaults

前端 未结 2 1375
-上瘾入骨i
-上瘾入骨i 2021-01-22 03:44

I have an html helper that I\'d like to set a default on.

@Html.EditorFor(model => model.DateFrom)

What\'s the syntax to set the default val

2条回答
  •  再見小時候
    2021-01-22 04:13

    I dont think that using EditorFor you can set a default value. Consider setting it in the accessors on your model?

    To do it on other types ( TextBoxFor etc ) You can set a value but not a default value. So you would need to do:

    @if(Model.something == null)
    {
       @Html.TextBoxFor(m => m.ID, new { @Value = "Value!"})
    } else {
       @Html.TextBoxFor(m => m.ID)
    }
    

    As I would reccomend:

    private DateTime? _date;
    public DateTime? date {
    get {
       if(_date == null)
          _date = DateTime.Now;
       return _date;
    }
    set {
       _date = value;
    }
    }
    

    Using things such as jquery date picker allow you to have a default value if the problem is that you are just posting nothing back if it has not been selected.

提交回复
热议问题