Results 1 to 5 of 5

Thread: default value of todays date

  1. #1
    BitLost is offline C# Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2016
    Posts
    21
    Rep Power
    6

    Angry default value of todays date

    I feel like I am looking for the wrong thing...and probably am.

    I have some code in my app to allow the user to input the inspection date.

    Code:
    <divclass="form-group"> <label asp-for="InspectDate" class="col-md-2 control-label"></label>
                   <div class="col-md-10">
                       <input asp-for="InspectDate" class="form-control" value="@DateTime.Today"/>
                       <span asp-validation-for="InspectDate" class="text-danger" />
                   </div> </div>
    All I am wanting to do is insert a default value of todays date. It can be overwritten by the user if need as it is not critical. I keep thinking I should be able to do this in the view with some form of default for the input but...

  2. #2
    jmcilhinney is offline C# Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2011
    Posts
    1,435
    Rep Power
    116
    The value for that field is coming from your model, like all the other fields. If you want that field to show today's date then set that property in the model, i.e. InspectDate, to today's date.

  3. #3
    BitLost is offline C# Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2016
    Posts
    21
    Rep Power
    6
    Quote Originally Posted by jmcilhinney View Post
    The value for that field is coming from your model, like all the other fields. If you want that field to show today's date then set that property in the model, i.e. InspectDate, to today's date.
    Thanks...but wouldn't that also set a default in the database?

    This is part of a larger issue I am looking at. I thought date would be easiest to start with so choose that.

    This form has about 12 fields or so. 3 of these fields are common to all the data in this form at the time the user is taking the data. Therefore the intent was to add another button and have these fields retain value from last form to the next form. I thought this would be a controller action event. Am I confusing the two issues?

  4. #4
    jmcilhinney is offline C# Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2011
    Posts
    1,435
    Rep Power
    116
    It's a controller action that displays the view and it's that action that provides the model for the view, therefore it's that action that sets the default value of that property before passing the model to the view.

  5. #5
    BitLost is offline C# Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2016
    Posts
    21
    Rep Power
    6
    Quote Originally Posted by jmcilhinney View Post
    It's a controller action that displays the view and it's that action that provides the model for the view, therefore it's that action that sets the default value of that property before passing the model to the view.
    Thanks that makes much more sense to me. I wasn't looking at the controller that way.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •