Reputation: 566
i will like to generate list and bind into viewmodel but i was get error below, did i define wrong attribute?
The model item passed into the dictionary is of type 'SurveyTool.Models.AnswerQuestionViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[SurveyTool.Models.AnswerQuestionViewModel]'.
Edit.cshtml:
@model IEnumerable<SurveyTool.Models.AnswerQuestionViewModel>
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
<table>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Question)
</td>
<td>
@Html.EditorFor(modelItem => item.Answer)
</td>
</tr>
}
</table>
SURV_AnswerController:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SurveyTool.Models;
namespace SurveyTool.Controllers
{
public class SURV_AnswerController : Controller
{
private SurveyToolDB db = new SurveyToolDB();
//
// GET: /SURV_Answer/
public ActionResult Edit(int Survey_ID)
{
AnswerQuestionViewModel viewmodel = new AnswerQuestionViewModel();
var query = from r in db.SURV_Question_Ext_Model
join s in db.SURV_Question_Model
on r.Qext_Question_ID equals
s.Question_ID
where s.Question_Survey_ID == Survey_ID
orderby s.Question_Position ascending
select r;
foreach(var item in query)
{
viewmodel.Question = item.Qext_Text;
}
return View(viewmodel);
}
}
}
AnswerQuestionViewModel:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace SurveyTool.Models
{
public class AnswerQuestionViewModel
{
public string Answer { get; set; }
public string Question { get; set; }
}
}
Upvotes: 0
Views: 385
Reputation:
Your returning only a single AnswerQuestionViewModel
item, not a collection. In your public ActionResult Edit(int Survey_ID)
method, change
AnswerQuestionViewModel viewmodel = new AnswerQuestionViewModel();
to
List<AnswerQuestionViewModel> viewmodel = new List<AnswerQuestionViewModel>();
and then in the foreach
loop
foreach(var item in query)
{
viewmodel.Add(new AnswerQuestionViewModel() { Question = item.Qext_Text });
}
return View(viewmodel);
Edit
Note there are also problems with your view. You use of a foreach
loop generates duplicate name
attributes so the collection will not be bound on post back. It also generates invalid html because of the duplicate id
attributes. You need to use a for
loop of a custom EditorTemplate
for typeof AnswerQuestionViewModel
. Using a for
loop, it needs to be
@model List<SurveyTool.Models.AnswerQuestionViewModel>
@using (Html.BeginForm())
{
for(int i = 0; i < Model.Count; i++)
{
@Html.DisplayFor(m => m[i].Question)
@Html.EditorFor(m => m[i].Answer)
}
<input type="submit" />
}
However this will only post back the Answer
property of your model. You do not have any property identifying the ID of the question so you probably need an additional property for the ID, and include a hidden input for it in the view
Upvotes: 1