简体   繁体   English

C# 和 ASP.NET MVC:如何在另一个模型中填充数据对象模型

[英]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:我的存储过程返回:

  • QuestionId问题编号
  • Name姓名
  • QuestionTypeId问题类型 ID

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:我的存储过程返回:

  • QuestionTypeId问题类型 ID
  • Name姓名

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.

相关问题 如何在Javascript中使用模型(C#MVC ASP.NET) - How to use Model in Javascript (C# MVC ASP.NET) 如何在 C# Z9E0DA8438E1E38A1C30F4B76? - How to Create Model with Properties of Related Object in C# ASP.NET MVC? ASP.Net MVC C#模型与另一个模型的多个实例? - ASP.Net MVC C# model with multiple instances of another model? ASP.NET Core 5 MVC / C#:根据另一个 model 更改模型的值 - ASP.NET Core 5 MVC / C#: change model's value according to another model C#-如何在ASP.NET MVC的HTML表单中传递内部带有其他模型的模型 - C# - How do I pass a model with another model inside from a HTML form in ASP.NET MVC 如何在Asp.net MVC 5中填充没有实体框架的模型 - How to populate a model without entity framework in Asp.net MVC 5 如何从 Asp.Net MVC 中的视图填充模型 - How to populate in Model from View in Asp.Net MVC 我如何将现有模型(设备)数据附加到MVC ASP.NET中的另一个模型(用户)数据 - How can i attach existing model(device) data to another model(user) data in mvc asp.net 从下拉菜单中选择一种类型的模型,以在ASP.NET MVC中的另一种模型上填充属性 - Select one type of model from a Drop Down to populate a property on another model in ASP.NET MVC Asp.net MVC C#一种形式,两种模型 - Asp.net MVC C# One Form, Two Model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM