0

This is Task model

namespace TaskManager.Models
{
    public class Task
    {
        private const int DefaultTaskPriority = 1;

        //some other properties

        [DefaultValue(DefaultTaskPriority)]
        public TaskPriority Priority { get; set; }

    }

    public enum TaskPriority
    {
        [Display(Name = "Low")]
        Low = 0,

        [Display(Name = "Default")]
        Normal = 1,

        [Display(Name = "High")]
        High = 2,

        [Display(Name = "Extreme")]
        Extreme = 3
    }
}

and this is part of my view

<div class="form-group">
@Html.LabelFor(u => u.Priority)
<div class="radio">
    <label>@Html.RadioButtonFor(model => model.Priority, 0) Low</label>
</div>

<div class="radio">
    <label>@Html.RadioButtonFor(model => model.Priority, 1, new { Checked = "checked" }) Default</label>
</div>

<div class="radio">
    <label>@Html.RadioButtonFor(model => model.Priority, 2) High</label>
</div>

<div class="radio">
    <label>@Html.RadioButtonFor(model => model.Priority, 3) Extreme</label>
</div>
@Html.ValidationMessageFor(u => u.Description, null, new {@class = "error"})                    
</div>

but I'm not sure, is it the right way to create radio buttons? Maybe I should get label for every radio button from model (from enum TaskPriority)? I'm new in MVC so I don't know which way is better?

UPD View using values from enum TaskPriority

<div class="form-group">
@Html.LabelFor(u => u.Priority)
<div class="radio">
    <label>@Html.RadioButtonFor(model => model.Priority, TaskPriority.Low) Low</label>
</div>

<div class="radio">
    <label>@Html.RadioButtonFor(model => model.Priority, TaskPriority.Normal) Normal</label>
</div>

<div class="radio">
    <label>@Html.RadioButtonFor(model => model.Priority, TaskPriority.High) High</label>
</div>

<div class="radio">
    <label>@Html.RadioButtonFor(model => model.Priority, TaskPriority.Extreme) Extreme</label>
</div>
@Html.ValidationMessageFor(u => u.Description, null, new {@class = "error"})                    
</div>
Heidel
  • 3,174
  • 16
  • 52
  • 84
  • You should not be setting `new { Checked = "checked" }` - its the value of `Priority` which will determine what is selected (that's how model binding works. –  Nov 28 '15 at 09:36
  • 1
    You can also replace the int values with `TaskPriority.Low`, `TaskPriority.Normal` etc. And you can always create an extension method for getting the display name as per [this answer](http://stackoverflow.com/questions/13099834/how-to-get-the-display-name-attribute-of-an-enum-member-via-mvc-razor-code) –  Nov 28 '15 at 09:37
  • If I don't set `new { Checked = "checked" }` this radio button doesn't be selected, I want user see which option is selected by default. – Heidel Nov 28 '15 at 09:38
  • Wrong. If the value of `Priority` is `TaskPriority.Normal`, then the 2nd one will be selected (if you do not explicitly set it it will be `TaskPriority.Low` by default). Do NOT attempt to override the model binding features of MVC –  Nov 28 '15 at 09:40
  • Well, I tried to remove `new { Checked = "checked" }` and radio button with `TaskPriority.Normal` became not checked now. – Heidel Nov 28 '15 at 09:42
  • Have you set the value of `Priority` to `model.Priority = TaskPriority.Normal;` before you return the view to the model? –  Nov 28 '15 at 09:46
  • I added in my post new code from view – Heidel Nov 28 '15 at 09:50
  • 1
    The other suggestion would be to add `new { id = '' }` to remove the duplicate `id` attributes from each radio button and `@Html.LabelFor(u => u.Priority)` is a bit pointless (you already have a label associated with each button) so it should be `@Html.DisplayNameFor(m => m.Priority)` –  Nov 28 '15 at 09:54
  • Great suggestion, I will use it, thanks! – Heidel Nov 28 '15 at 10:09

0 Answers0