0

I'm building an application form, and have three models currently setup to handle all the data and are linked with my DBMS.

  • ApplicationSubmission.cs: this contains a ID, character name & a date added field
  • ApplicationQuestion.cs: this contains the data structure and pulls through the questions from the database, which I'll be using to display on the front-end of the site
  • ApplicationAnswer.cs: this contains an ID, application_id, question_id & answer field. application_id & question_id need to link with the ID's in the above two models.

ApplicationSubmission.cs

public partial class ApplicationSubmission
{
    public long id { get; set; }

    [Column(TypeName = "text")]
    public string character_name { get; set; }

    public DateTime? date_added { get; set; }
}

ApplicationQuestion.cs

public partial class ApplicationQuestion
{
    public int id { get; set; }

    [Column(TypeName = "text")]
    public string question { get; set; }

    [Column(TypeName = "text")]
    public string description { get; set; }
}

ApplicationAnswer.cs

public partial class ApplicationAnswer
{
    public int id { get; set; }

    public int application_id { get; set; }

    public int question_id { get; set; }

    [Column(TypeName = "text")]
    public string answer { get; set; }
}

My question is: how would I go about implementing these in the razor view, so that I can pull through the question name / description, but have the posted data go into a new ApplicationAnswer object, but reference the question_id that it's related to?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Josh Walshaw
  • 200
  • 1
  • 2
  • 17
  • 2
    How about creating a single viewmodel which contains all required properties from those model classes? See also: [multiple models in view](https://stackoverflow.com/questions/4764011/multiple-models-in-a-view). – Tetsuya Yamamoto Aug 07 '18 at 07:19
  • Generally razor doesn't support this use case on by itself. What would you normally do: make the model you submit as your razor view model and then get all the questions and other data via ajax requests and populate it with javascript. Alternatively, if you are using asp.net core, you would want to use 'view component' in this case. – Tomas Smagurauskas Aug 07 '18 at 07:26

1 Answers1

0

Perhaps you could do something like this...

public partial class ApplicationAnswer
{

    public ApplicationQuestion question { get; set; }
    public ApplicationSubmission submission { get; set; }

    public int id { get; set; }

    public int application_id { get; set; }

    public int question_id { get; set; }

    [Column(TypeName = "text")]
    public string answer { get; set; }
}

Then your view would just accept an ApplicationAnswer model, but from this model, it can get to the Submission and Question properties as well.

user1751825
  • 4,029
  • 1
  • 28
  • 58