Search code examples
asp.net-mvc-4editorfor

EditorFor isn't using EditorTemplate in MVC4


I'v been bashing my head against a wall trying to get EditorFor working as described here, but I can't for the life of me get my version to work.

I have two ViewModels:

InterviewViewModel

public class InterviewViewModel
{
    ...
    public List<QuestionViewModel> Questions { get; set; }
}

and QuestionViewModel:

public class QuestionViewModel
{
    public int QuestionId { get; set; }
    public string QuestionName { get; set; }
    ...
}

I have tried creating EditorTemplates in two places,

/Views/Interview/EditorTemplates/QuestionViewModel.cshtml

and

/Views/Shared/EditorTemplates/QuestionViewModel.cshtml

Neither one appears to do anything.

Controller code:

public ActionResult MyClass(int id = 0)
    {
        using (RRContext db = new RRContext())
        {
            ...

            List<QuestionModel> questionModels = new List<QuestionModel>();
            questionModels = db.QuestionModels.ToList();

            ...

            viewModel.Questions = questionViewModels;

        return View(viewModel);
        }
    }

My View contains:

@model ResidentRank.Models.Interview.InterviewViewModel

... (html.beginform is here)
Html.EditorFor(model => model.Questions);      

EditorTemplate code:

@model ResidentRank.Models.Interview.QuestionViewModel
<div style="clear:both; margin:10px 0px;">
  <label>@Html.DisplayFor(m => m.QuestionName)</label>
  @Html.DropDownListFor(m => m.SelectedQuestionOption, Model.OptionSelector)
</div>

The question banging around in my head is "WHY IS THIS NOT WORKING?!"


Solution

  • In HomeController

        [HttpGet]
        public ActionResult Display(int id = 0)
        {
            var questionViewModel = new InterviewViewModel {
                Questions =
                    new List<QuestionViewModel>()
                    {
                        //Hard coded values to represent data coming from db i.e db.QuestionModels.ToList();
                        new QuestionViewModel() {QuestionId = 1, QuestionName = "A"},
                        new QuestionViewModel() {QuestionId = 2, QuestionName = "B"}
                    }
            };
    
            return View(questionViewModel);
        }
    

    ViewModels

      public class InterviewViewModel {
        public List<QuestionViewModel> Questions { get; set; }
      }
    
      public class QuestionViewModel {
        public int QuestionId { get; set; }
        public string QuestionName { get; set; }
      }
    

    \Views\Home\Display.cshtml

      @model MvcApplication1.Models.Interview.InterviewViewModel
    
      @using (Html.BeginForm()) 
      {
          <fieldset>
             @Html.EditorFor(x => x.Questions)
             <input type="submit" value="Save" />
          </fieldset>
      } 
    

    \Views\Shared\EditorTemplates\QuestionViewModel.cshtml

    Make sure this is a partial view.

      @model MvcApplication1.Models.Questions.QuestionViewModel
      <div style="clear:both; margin:10px 0px;">
         <label>@Html.DisplayFor(m => m.QuestionName)</label>
      </div>
    

    Note that I removed the

      @Html.DropDownListFor(m => m.SelectedQuestionOption, Model.OptionSelector)
    

    for to keep the view simple.

    This displays..

    enter image description here