In Company model, I have:
[Display(Name = "Offered Services")]
public string[] Services { get; set; }
and I have a standard scaffold controller.
Now, in Razor view I need to use the checkbox helper which auto binds the values to model such that the following markup is generated for browser:
<div class="editor-label">
Offered Services
</div>
<div class="editor-field">
<input type="checkbox" name="Services[]" id="services-safety" value="safety" />
<label for="services-safety">Safety</label>
<input type="checkbox" name="Services[]" id="services-healthy" value="healthy" />
<label for="services-healty">Healthy</label>
... <!-- more checkboxes in future -->
</div>
Questions:
Is there any helper which binds it to model so the selected values are stay
checked
in edit view. Would it be possible without using extra ViewModel and controller code?Is it a good idea to let the presenter change the behavior of application? If someone change the checkbox value from client-side, then Service array will contain garbage data. In this case what is the best approach to deal with checkboxes to prevent it from such noise:
- ViewModel for polymorphic service names
- Enum Service {}
- Create separate properties for each service in Company model
class Service{}: containing all services as properties:
A) then make a property of the type Services in the model.
B) Make Service as a entity and make association with Company.