Set default value in MVC web application.

I am trying to set a default value for a field in an ASP.net MVC web application.

I use a database first, so I added a partial class for metadata like this:

[MetadataType(typeof(RadioRoutingMetadata))]
public partial class RadioRouting
{
}

public partial class RadioRoutingMetadata
{
    [DefaultValue("%")]
    public string Slot { get; set; }

    [Required(ErrorMessage = "This field is requied")]
    [DefaultValue(0)]
    public int BlockStart { get; set; }

    [Required(ErrorMessage = "This field is requied")]
    [DefaultValue(499)]
    public int BlockEnd { get; set; }

    [DefaultValue(-1)]
    public int FallBackBaseIdentifier { get; set; }
}

      

After reading, I can't see what [DefaultValue(T)]

does not initialize this field on creation. But the Html helper methods don't look at this field?

here is my view:

<p>
   @Html.LabelFor(model => model.Slot, htmlAttributes: new { @class = "control-label col-md-2" })
   <span class="field">
       @Html.EditorFor(model => model.Slot, new { htmlAttributes = new { @class = "form-control" } })
       @Html.ValidationMessageFor(model => model.Slot, "", new { @class = "text-danger" })
   </span>
</p>

<p>
    @Html.LabelFor(model => model.BlockStart, htmlAttributes: new { @class = "control-label col-md-2" })
    <span class="field">
        @Html.EditorFor(model => model.BlockStart, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.BlockStart, "", new { @class = "text-danger" })
    </span>
</p>

<p>
    @Html.LabelFor(model => model.BlockEnd, htmlAttributes: new { @class = "control-label col-md-2" })
    <span class="field">
        @Html.EditorFor(model => model.BlockEnd, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.BlockEnd, "", new { @class = "text-danger" })
    </span>
</p>

      

So, when I provide the Create form, I want these defaults to be on the form.

Do I need to initialize the Model object in the controller, set the defaults, and then pass it to the create view as if it were an edit view?

If so, how can I create a constructor that initializes all default values ​​in the partial class?

+3


source to share


2 answers


DefaultValue attribute

not used to set default values ​​for properties as you want. In fact, it is not used directly at runtime. It is intended instead for use by the Visual Studio designer.

More details here:

http://support.microsoft.com/kb/311339

OR

.Net DefaultValueAttribute on Properties




You can easily set default value for fields in MVC like:

 @Html.EditorFor(model => model.BlockEnd, new { htmlAttributes = new { @class = "form-control", @Value = "499" } })

      

Now with the above code, the first time the form is loaded, the initial value BlockEnd

will be499

+8


source


I suggest creating a viewmodel class and using it to set the default, eg.

public class ExampleViewModel
{
    public string Slot { get; set; }

    [Required(ErrorMessage = "This field is required")]
    public int BlockStart { get; set; }

    // Include other properties as required.
}

      

In your controller, use the viewmodel like this:

public ActionResult Index()
{
    var viewModel = new ExampleViewModel();
    viewModel.Slot = "A default value";

    return View(viewModel);
}

[HttpPost]
public ActionResult Index(ExampleViewModel viewModel)
{
    // Do whatever you need to do with the values in viewModel e.g. save them to database.
}

      

Looking at your view code, it doesn't seem like you have to change it, other than being placed on top

@model ExampleViewModel

      



I recommend using viewmodels rather than passing actual data classes into views as it makes it much easier to change your code. Also, the added flexibility you get from using viewmodels is a plus for example. using a data class like Car makes things difficult if you also want to display data from another data class like Driver on the same view. Using a view model called "VehicleDetailsViewModel", you can have properties such as "Driver Name", "CarMake", "CarModel", etc.

EDIT: Ahh, OK, so you are already using the viewmodel. In this case, I suggest you set the default values ​​in the constructor of the viewmodel class.

public ExampleViewModel()
{
    this.Slot = "Whatver";
    // etc.
}

      

I think this is much better than my previous suggestion to set a default in the above action method. But you have a choice of choice where you want to set the default, it is not set in stone.

I'm not sure if it is actually DefaultValue

picked up by the Razor parser.

+3


source







All Articles