Default value in an asp.net mvc view model

asp.net Mvcasp.net Mvc-3

asp.net Mvc Problem Overview


I have this model:

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

But based on my research and answers here, DefaultValueAttribute does not actually set a default value. But those answers were from 2008, Is there an attribute or a better way than using a private field to set these values to true when passed to the view?

Heres the view anyways:

@using (Html.BeginForm("Search", "Users", FormMethod.Get))
{
<div>
    @Html.LabelFor(m => Model.IsMale)
    @Html.CheckBoxFor(m => Model.IsMale)
    <input type="submit" value="search"/>
</div>
}

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

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());
}

Solution 2 - asp.net Mvc

Use specific value:

[Display(Name = "Date")]
public DateTime EntryDate {get; set;} = DateTime.Now;//by C# v6

Solution 3 - asp.net Mvc

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; }
}

Solution 4 - asp.net Mvc

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" })

Solution 5 - asp.net Mvc

<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

Solution 6 - asp.net Mvc

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShawn McleanView Question on Stackoverflow
Solution 1 - asp.net MvcDismissileView Answer on Stackoverflow
Solution 2 - asp.net MvcGuruprakashView Answer on Stackoverflow
Solution 3 - asp.net MvcCodeGrueView Answer on Stackoverflow
Solution 4 - asp.net MvcOleksii AzaView Answer on Stackoverflow
Solution 5 - asp.net Mvcuser14006190View Answer on Stackoverflow
Solution 6 - asp.net Mvcuser338195View Answer on Stackoverflow