Controlling default values in view from model - EF CORE

asp.net-core-mvc entity-framework-core

Question

I've set the default for a Boolean in the model using both

    [DefaultValue(true)]

and

public bool IsActive { get; set; } = true;

and for good measure I've added

    modelBuilder.Entity<modelName>().Property(p => p.IsActive).HasDefaultValue(true);

That all works nicely and my database table has the correct "Default value or binding". In my Create view I want the default property to be used but can't find out how to use the model's default properties in the view. Using

<input asp-for="IsActive"  class="form-control" checked="checked"/>

works, but that's me hardcoding the checked="checked" value myself. How do I read the model's default value in the view?

Edited for requested code, the model is

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace securitytrials.Models {
    public class CaseType {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int TheID { get; set; }
        [Display(Name = "Active")]
        public bool IsActive { get; set; } = true;
    }
}

the action method is the default generated by the template.

public IActionResult Create() {
    return View();
}

Didn't include them as there's not much there but the defaults. The DbContext.OnModelCreating is just

protected override void OnModelCreating(ModelBuilder modelBuilder) {
        modelBuilder.Entity<CaseType>().Property(p => p.IsActive).HasDefaultValue(true);
    }
1
0
11/17/2017 4:37:23 PM

Popular Answer

DefaultValue doesn't actually do anything. I can be utilized by code generators in certain situations, but largely in the context of your web application, it's completely meaningless. As the docs detail:

A DefaultValueAttribute will not cause a member to be automatically initialized with the attribute's value. You must set the initial value in your code.

HasDefaultValue merely sets the DEFAULT for the column, so you may need that as well, if you want the actual table schema to reflect the default, but the only thing that really helps your code is:

public bool IsActive { get; set; } = true;

It's not entirely clear what you're asking. As there should be no issue with that alone in an input displaying that default value. In other words, the following code:

<input asp-for="IsActive" class="form-control" />

Will generate:

<input type="textbox" class="form-control" checked="checked" />

If it's not, then there's some issue with your model. Either you're confused on which class you've added the default value to, or something else in your code is changing that value before it hits your view. However, without more code to work with, like your action method, it's impossible to say.

0
11/17/2017 3:31:58 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow