从.NET Core MVC中的另一个模型中插入数据中的数据
我正在尝试构建一个测验应用程序,我想在提交测验后插入用户的答案。 这是我的问题模型:
public class Question
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Display(Name = "Id Question")]
public int id_question { get; set; }
[Column(TypeName = "varchar(100)")]
[Display(Name = "Question")]
public string question { get; set; }
public string option1 { get; set; }
public string option2 { get; set; }
public string option3 { get; set; }
public string option4 { get; set; }
public string answer { get; set; }
public virtual List<Result> Results { get; set; }
}
这是我想要存储用户答案的结果模型:
public class Result
{
[Key]
[Display(Name ="Id Result")]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id_result { get; set; }
[ForeignKey("User")]
[Required]
public string Id_User { get; set; }
[Display(Name = "user")]
[NotMapped]
public string User_Name { get; set; }
[ForeignKey("Question")]
[Required]
public int id_question { get; set; }
[Display(Name = "Question")]
[NotMapped]
public string q { get; set; }
public string UserAnswer { get; set; }
public virtual ApplicationUser User { get; set; }
public virtual Question Question { get; set; }
}
在数据库中存储考试问题后,我想像这样显示它们并在提交后存储答案:
@model IEnumerable<Exam.Areas.Identity.Data.Question>
@{
ViewData["Title"] = "Exam";
}
<div>
<h4>Question</h4>
<hr />
@foreach (var item in Model)
{
<div class="row pl-5">
<div class="col-md-4">
<div>@Html.DisplayFor(modelItem => item.question)</div>
<div class="form-check">
<input class="form-check-input" type="radio" value="@item.option1" name="@item.id_question" id="">
<label class="form-check-label" for="">
@Html.DisplayFor(modelItem => item.option1)
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" value="@item.option2" name="@item.id_question" id="">
<label class="form-check-label" for="">
@Html.DisplayFor(modelItem => item.option2)
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" value="@item.option3" name="@item.id_question" id="">
<label class="form-check-label" for="">
@Html.DisplayFor(modelItem => item.option3)
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" value="@item.option4" name="@item.id_question" id="">
<label class="form-check-label" for="">
@Html.DisplayFor(modelItem => item.option4)
</label>
</div>
</div>
</div>
}
我尝试了很多事情徒然。谁能帮我想出一种方法来解决这个问题?
I'm trying to build a quiz application and i want to insert the user's answers after the quiz is submitted.
this is my question model:
public class Question
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Display(Name = "Id Question")]
public int id_question { get; set; }
[Column(TypeName = "varchar(100)")]
[Display(Name = "Question")]
public string question { get; set; }
public string option1 { get; set; }
public string option2 { get; set; }
public string option3 { get; set; }
public string option4 { get; set; }
public string answer { get; set; }
public virtual List<Result> Results { get; set; }
}
And this is the Result model where i want to store the user's answers:
public class Result
{
[Key]
[Display(Name ="Id Result")]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id_result { get; set; }
[ForeignKey("User")]
[Required]
public string Id_User { get; set; }
[Display(Name = "user")]
[NotMapped]
public string User_Name { get; set; }
[ForeignKey("Question")]
[Required]
public int id_question { get; set; }
[Display(Name = "Question")]
[NotMapped]
public string q { get; set; }
public string UserAnswer { get; set; }
public virtual ApplicationUser User { get; set; }
public virtual Question Question { get; set; }
}
After storing an exam's questions in the database I want to display them like this and store the answers after submitting them:
@model IEnumerable<Exam.Areas.Identity.Data.Question>
@{
ViewData["Title"] = "Exam";
}
<div>
<h4>Question</h4>
<hr />
@foreach (var item in Model)
{
<div class="row pl-5">
<div class="col-md-4">
<div>@Html.DisplayFor(modelItem => item.question)</div>
<div class="form-check">
<input class="form-check-input" type="radio" value="@item.option1" name="@item.id_question" id="">
<label class="form-check-label" for="">
@Html.DisplayFor(modelItem => item.option1)
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" value="@item.option2" name="@item.id_question" id="">
<label class="form-check-label" for="">
@Html.DisplayFor(modelItem => item.option2)
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" value="@item.option3" name="@item.id_question" id="">
<label class="form-check-label" for="">
@Html.DisplayFor(modelItem => item.option3)
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" value="@item.option4" name="@item.id_question" id="">
<label class="form-check-label" for="">
@Html.DisplayFor(modelItem => item.option4)
</label>
</div>
</div>
</div>
}
I've tried many things in vain. Can anyone help me figure out a way to approach this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您回答的Rahul Sharma的评论来看,您的问题可能是如何将答案存储在结果表中。
如果我们想回答多个问题并将它们提交在一个页面中,则可以将问题和相应的答案传递给控制器作为列表集合,然后将它们存储在数据库中以进行调用。
如果允许调用多个模型加载数据,则可以参考以下示例代码。
模型:
Question.cs:
result.cs:
applicationuser.cs:
控制器:
QuestionAndResultController.cs:
视图:
index.cshtml:
测试结果:
索引页:
单击“答案”:
单击“提交”:
Judging by Rahul Sharma's comment you responded to, your question might be how to store the answer in the Results table.
If we want to answer multiple questions and submit them in one page, we can pass the questions and corresponding answers back to the controller as a List collection and store them in the database to call.
If you allow calling multiple models to load data, you can refer to the following sample code.
Models:
Questions.cs:
Result.cs:
ApplicationUser.cs:
Controller:
QuestionAndResultController.cs:
View:
Index.cshtml:
Test Result:
Index page:
Click on "answer":
Click on "Submit":