[英]C# & ASP.NET MVC: How to populate a data object model within another model
I have a model that handles questions to display on an ASP.NET MVC form.我有一个模型来处理要在 ASP.NET MVC 表单上显示的问题。 One of the properties within my
Question
model is the type of question the item is ( QuestionType
);我的
Question
模型中的属性之一是项目的问题类型( QuestionType
); this is its own separate model class, however my problem is I'm having trouble reading the QuestionType
values from the database and then updating the QuestionType
property within my Question
model class.这是它自己单独的模型类,但是我的问题是我无法从数据库中读取
QuestionType
值,然后在我的Question
模型类中更新QuestionType
属性。
For example, I'd like to be able to do the following, but I don't know how to populate my QuestionType
correctly:例如,我希望能够执行以下操作,但我不知道如何正确填充我的
QuestionType
:
string myQuestionTypeName = model.Question.QuestionType.Name;
Question
model class: Question
模型类:
public class Question
{
public int QuestionId { get; set; }
public string Name { get; set; }
public QuestionType QuestionType { get; set; }
}
QuestionType
model class: QuestionType
模型类:
public class QuestionType
{
public int QuestionTypeId { get; set; }
public string Name { get; set; }
}
Question
data - pulls from my Question
database table. Question
数据 - 从我的Question
数据库表中提取。
My stored procedure returns:我的存储过程返回:
Code:代码:
public List<Question> PullList()
{
List<Question> questionList = new List<question>();
using (Sql sql = new Sql(Sql.Connection.MyDbConnectionName, "storedProc_PullQuestions"))
{
using (var ds = sql.GetDataSet())
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
Question question = new Question();
question.QuestionId = dr.Field<int>("QuestionId");
question.Name = dr.Field<string>("Name");
question.QuestionType = //??? Not sure what to do here. I have the "QuestionTypeId" from the stored procedure, but not sure how to populate this object based on that id.
questionList.Add(question);
}
}
}
return questionList;
}
QuestionType
data - pulls from my QuestionType
table; QuestionType
数据 - 从我的QuestionType
表中提取; my stored procedure returns:我的存储过程返回:
Code:代码:
public List<QuestionType> PullList()
{
List<QuestionType> questionTypeList = new List<QuestionType>();
using (Sql sql = new Sql(Sql.Connection.MyDbConnectionName, "storedProc_PullQuestionTypes"))
{
using (var ds = sql.GetDataSet())
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
QuestionType questionType = new QuestionType();
questionType.QuestionTypeId = dr.Field<int>("QuestionTypeId");
questionType.Name = dr.Field<string>("Name");
questionTypeList.Add(questionType);
}
}
}
return questionTypeList;
}
Thanks for any help you can provide感谢您的任何帮助,您可以提供
You have to join 2 tables in your select query of stored procedure and after this您必须在存储过程的选择查询中加入 2 个表,然后在此之后
foreach (DataRow dr in ds.Tables[0].Rows)
{
Question question = new Question();
question.QuestionType = new QuestionType();
question.QuestionId = dr.Field<int>("QuestionId");
question.Name = dr.Field<string>("Name");
question.QuestionType.QuestionTypeId = dr.Field<int>"QuestionTypeId");
question.QuestionType.Name = dr.Field<string>("QuestionTypeName");
questionList.Add(question);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.