我是Linq和EF Code First的新手,遇到了一个我无法弄清楚的问题。我正在使用ASP.Net Web Forms,EF Code First和Linq开发概念验证测试程序。我的想法是我根据数据库动态生成页面的服务器控件。在没有EF的情况下,我已经构建了相同的应用程序但是希望在这个项目上学到这一点。如果我使用文字但在数据绑定RadioButtonList时收到错误,我可以得到正确的结果。
DataBinding: 'System.Data.Entity.DynamicProxies.Choice_EA448AD48C19F54FBB6BF09B7A03BA899DBE75EC189635A8982E7C3B1D8F4ABD' does not contain a property with the name '4'.
Line 34: content.Controls.Add(ql);
Line 35: ql.DataSource = choices;
Line 36: ql.DataBind();
Line 37: }
名称为' 4'只是我的数据库中的值。我不确定这个问题是否与延迟加载有关,或者只是缺乏适当的Linq知识,但如果有人能指出我正确的方向,我会非常感激。
这是我正在使用的三个类文件和我正在尝试构建的C#页面。请记住,我计划将数据访问分成另一个类,但只是试图让我自己的演示工作。 Exam.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
/// <summary>
/// Summary description for Exam
/// </summary>
public class Exam
{
public int ExamId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public bool Status { get; set; }
public DateTime DateCreated { get; set; }
public string CreatedBy { get; set; }
public virtual ICollection<Question> Questions { get; set; }
}
Question.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Question
/// </summary>
public class Question
{
public int QuestionId { get; set; }
public string QuestionText { get; set; }
public int Order { get; set; }
public DateTime DateCreated { get; set; }
public bool Status { get; set; }
public string CreatedBy { get; set; }
public string QuestionType { get; set; }
public int ExamId { get; set; }
public virtual Exam Exam { get; set; }
public virtual ICollection<Choice> Choices { get; set; }
}
Choice.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Choice
/// </summary>
public class Choice
{
public int ChoiceId { get; set; }
public string ChoiceText { get; set; }
public int Order { get; set; }
public bool Status { get; set; }
public bool Correct { get; set; }
public DateTime DateCreated { get; set; }
public string CreatedBy { get; set; }
public int QuestionId { get; set; }
public virtual Question Question { get; set; }
}
Testing.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Testing : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ExamsContext db = new ExamsContext();
var questions = (from c in db.Exams
from q in c.Questions
where c.ExamId == 1
select q).ToList();
ContentPlaceHolder content = (ContentPlaceHolder)this.Master.FindControl("MainContent");
foreach (var question in questions)
{
Literal questionLabel = new Literal();
questionLabel.Text = Convert.ToString(question.QuestionId) + ". " + question.QuestionText + "<br/>";
content.Controls.Add(questionLabel);
var choices = (from c in question.Choices select c).ToList();
RadioButtonList ql = new RadioButtonList();
foreach (var choice in choices)
{
ql.DataValueField = Convert.ToString(choice.ChoiceId);
ql.DataTextField = choice.ChoiceText;
}
content.Controls.Add(ql);
ql.DataSource = choices;
ql.DataBind();
}
}
}
答案 0 :(得分:2)
我使用ASP.NET WebForms已经有一段时间了,但我认为你的问题出在这里:
ql.DataValueField = Convert.ToString(choice.ChoiceId);
ql.DataTextField = choice.ChoiceText;
我认为Convert.ToString(choice.ChoiceId)
告诉控件使用名为&#34; 4&#34;的属性。
试试这个:
ql.DataValueField = "ChoiceId";
ql.DataTextField = "ChoiceText";
即。您需要指定要使用的属性的名称,而不是值