在viewmodel上生成一个List

时间:2015-05-28 06:33:29

标签: c# linq list asp.net-mvc-4 razor

我想生成列表并绑定到viewmodel但是我在下面得到错误,我是否定义了错误的属性?

  

传递到字典中的模型项是类型的   'SurveyTool.Models.AnswerQuestionViewModel',但是这本字典   需要类型的模型项   '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; }
    }
}

1 个答案:

答案 0 :(得分:1)

您只返回一个@Entity @Table(name = "AW_MEDIA_DETAILS") public class MediaDetails implements WFPayload , Serializable{ /** * */ private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name = "REQUEST_ID", unique = true, nullable = false) Long requestId; .... 项,而不是一个集合。在AnswerQuestionViewModel方法中,更改

public ActionResult Edit(int Survey_ID)

AnswerQuestionViewModel viewmodel = new AnswerQuestionViewModel();

然后在List<AnswerQuestionViewModel> viewmodel = new List<AnswerQuestionViewModel>(); 循环

foreach

修改

请注意,您的观点也存在问题。您使用foreach(var item in query) { viewmodel.Add(new AnswerQuestionViewModel() { Question = item.Qext_Text }); } return View(viewmodel); 循环生成重复的foreach属性,因此集合不会在回发时绑定。由于重复的name属性,它还会生成无效的html。您需要为id类型使用自定义for的{​​{1}}循环。使用EditorTemplate循环,它必须是

AnswerQuestionViewModel

但是,这只会回发模型的for属性。您没有任何标识问题ID的属性,因此您可能需要ID的附加属性,并在视图中包含隐藏的输入