I have a dropdown that represents various "attribute" fields related to a document. For example, a document could have a Tags field, which would require a text-box to enter your tag(s) into. Or it might have a Location field, and that would have a pre-populated dropdown of various location types the user could pick. Or it could be a Deadline field, and we have a custom date-picker thing we use for picking a date.
My problem: I wanted to declare an HtmlHelper inside my controller action that fires when selecting the appropriate attribute field. I would switch against what the Attribute.Type is, and depending on the type, I would return in my view model the appropriate html to render in the view. If I did it this way I can unit test my switch statement.
I am reading all over that declaring an HtmlHelper in a controller is a bad idea. I can understand the sentiment-- is there a better way I can do this? I want to avoid logic in my view whenever possible, because I can't unit test that at all.
The other way I see of doing this is passing the Attribute.Type along in the view model, and doing the switch logic there on the view to determine which HtmlHelper method to call. Is that the way to go? Thanks in advance.
EDIT: I'm talking about wanting to do something like this in the code-behind. My unit test would assert against viewModel.FieldHtml. What I'm hearing is that I should put this in another class and have the controller call it. But here's what it would have looked like in the controller. It's just pseudo-code so it's not perfect but it's to give context to what I'm asking.
public ActionResult GetValueInput(Guid attributeFieldUid)
{
//you have to pass some stuff into the constructor but essentially this
HtmlHelper html = new HtmlHelper();
AttributeField field = GetAttributeFieldFromDb(attributeFieldUid);
AttributeViewModel viewModel = new AttributeViewModel();
switch(field.type)
{
case Dropdown:
viewModel.FieldHtml = html.DropDownList();
break;
case Text:
viewModel.FieldHtml = html.TextBox();
break;
case Date:
// our own extension method
viewModel.FieldHtml = html.OurDatePicker();
break;
}
}
The other option I initially saw was to essentially do this switch statement on a razor view. But I think I like the idea of doing this in a separate class the controller calls the most.
This is really a design question-- I'm asking what way to implement this makes the most sense?