Default value in an asp.net mvc view model

后端 未结 6 554
星月不相逢
星月不相逢 2020-12-05 01:37

I have this model:

public class SearchModel
{
    [DefaultValue(true)]
    public bool IsMale { get; set; }
    [DefaultValue(true)]
    public bool IsFemale         


        
相关标签:
6条回答
  • 2020-12-05 01:51

    Use specific value:

    [Display(Name = "Date")]
    public DateTime EntryDate {get; set;} = DateTime.Now;//by C# v6
    
    0 讨论(0)
  • 2020-12-05 01:51

    Create a base class for your ViewModels with the following constructor code which will apply the DefaultValueAttributeswhen any inheriting model is created.

    public abstract class BaseViewModel
    {
        protected BaseViewModel()
        {
            // apply any DefaultValueAttribute settings to their properties
            var propertyInfos = this.GetType().GetProperties();
            foreach (var propertyInfo in propertyInfos)
            {
                var attributes = propertyInfo.GetCustomAttributes(typeof(DefaultValueAttribute), true);
                if (attributes.Any())
                {
                    var attribute = (DefaultValueAttribute) attributes[0];
                    propertyInfo.SetValue(this, attribute.Value, null);
                }
            }
        }
    }
    

    And inherit from this in your ViewModels:

    public class SearchModel : BaseViewModel
    {
        [DefaultValue(true)]
        public bool IsMale { get; set; }
        [DefaultValue(true)]
        public bool IsFemale { get; set; }
    }
    
    0 讨论(0)
  • 2020-12-05 01:53

    In case you need to post the same model to server the solution with having default bool value in constructor would not be viable for you. Let's imagine that you have following model:

    public class SearchModel
    {
        public bool IsMale { get; set; }
    
        public SearchModel()
        { 
            IsMale = true;
        }
    }
    

    On view you would have something like this:

    @Html.CheckBoxFor(n => n.IsMale)
    

    The problem is when user uncheck this checkbox and post it to the server - you would end up with default value set up in constructor (which in this case is true).

    So in this case I would end up with just specifying default value on view:

    @Html.CheckBoxFor(n => n.IsMale, new { @checked = "checked" })
    
    0 讨论(0)
  • 2020-12-05 02:00
    <div class="form-group">
                        <label asp-for="Password"></label>
                        <input asp-for="Password"  value="Pass@123" readonly class="form-control" />
                        <span asp-validation-for="Password" class="text-danger"></span>
                    </div>
    

    use : value="Pass@123" for default value in input in .net core

    0 讨论(0)
  • 2020-12-05 02:12

    Set this in the constructor:

    public class SearchModel
    {
        public bool IsMale { get; set; }
        public bool IsFemale { get; set; }
    
        public SearchModel()
        { 
            IsMale = true;
            IsFemale = true;
        }
    }
    

    Then pass it to the view in your GET action:

    [HttpGet]
    public ActionResult Search()
    {
        return new View(new SearchModel());
    }
    
    0 讨论(0)
  • 2020-12-05 02:12

    What will you have? You'll probably end up with a default search and a search that you load from somewhere. Default search requires a default constructor, so make one like Dismissile has already suggested.

    If you load the search criteria from elsewhere, then you should probably have some mapping logic.

    0 讨论(0)
提交回复
热议问题