Я бился головой о стену, пытаясь заставить EditorFor работать, как описано здесь, но я не могу заставить свою версию работать.
У меня есть две модели ViewModel:
InterviewViewModel
public class InterviewViewModel
{
...
public List<QuestionViewModel> Questions { get; set; }
}
И QuestionViewModel :
public class QuestionViewModel
{
public int QuestionId { get; set; }
public string QuestionName { get; set; }
...
}
Я пробовал создавать EditorTemplates в двух местах:
/Views/Interview/EditorTemplates/QuestionViewModel.cshtml
А также
/Views/Shared/EditorTemplates/QuestionViewModel.cshtml
Кажется, что ни один из них ничего не делает.
Код контроллера:
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 содержит:
@model ResidentRank.Models.Interview.InterviewViewModel
... (html.beginform is here)
Html.EditorFor(model => model.Questions);
Код EditorTemplate:
@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>
В голове крутится вопрос: «ПОЧЕМУ ЭТО НЕ РАБОТАЕТ ?!»
1 ответ
В 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; }
}
\ Виевс \ Главная \ 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
Убедитесь, что это частичный вид.
@model MvcApplication1.Models.Questions.QuestionViewModel
<div style="clear:both; margin:10px 0px;">
<label>@Html.DisplayFor(m => m.QuestionName)</label>
</div>
Обратите внимание, что я удалил
@Html.DropDownListFor(m => m.SelectedQuestionOption, Model.OptionSelector)
Для упрощения просмотра.
Это отображает ..
Похожие вопросы
Связанные вопросы
Новые вопросы
asp.net-mvc-4
ASP.NET MVC 4 является четвертой основной версией платформы ASP.NET Model-View-Controller для веб-приложений.