I have an object which looks like this:
我有一个看起来像这样的对象:
public class QuestionSet
{
[Required]
public int Id { get; set; }
[Required]
public int ConferenceId { get; set; }
[Required]
public string Name { get; set; }
public virtual ICollection<RegistrationQuestion> Questions { get; set; }
public virtual ICollection<QuestionSetAssignee> QuestionSetAssignees { get; set; }
public QuestionSet()
{
}
public QuestionSet(int conferenceId)
{
this.ConferenceId = conferenceId;
}
}
And the model for RegistrationQuestion looks like this:
RegistrationQuestion的模型如下所示:
public class RegistrationQuestion
{
[Required]
public int Id { get; set; }
[Required]
public int QuestionSetId { get; set; }
[Required]
public string QuestionText { get; set; }
}
When creating a QuestionSet, I would like to create its related RegistrationsQuestions at the same time. However, the Razor Html Helpers don't let me drill down to that level.
在创建QuestionSet时,我想同时创建其相关的RegistrationsQuestions。但是,Razor Html Helpers不会让我深入到那个级别。
I can do this:
我可以做这个:
@Html.LabelFor(model => model.Questions)
but not this (and this is what I want to be able to do):
但不是这个(这是我想要做的):
@Html.LabelFor(model => model.Questions.QuestionText)
Any guidance on how to achieve this?
有关如何实现这一目标的任何指导?
2 个解决方案
#1
0
You can get it working like this. I made small changes to your model, instead of using ICollection<>, I used IList<> which have indexer support and can easily be iterated.
你可以让它像这样工作。我对你的模型做了一些小改动,而不是使用ICollection <>,我使用了IList <>,它有索引器支持,可以很容易地迭代。
public class QuestionSet
{
[Required]
public int Id { get; set; }
[Required]
public int ConferenceId { get; set; }
[Required]
public string Name { get; set; }
public virtual IList<RegistrationQuestion> Questions { get; set; }
}
public class RegistrationQuestion
{
[Required]
public int Id { get; set; }
[Required]
public int QuestionSetId { get; set; }
[Required]
public string QuestionText { get; set; }
}
I add following sample data in controller action -
我在控制器操作中添加以下示例数据 -
public ActionResult Index()
{
QuestionSet q = new QuestionSet();
q.Questions = new List<RegistrationQuestion>();
q.Questions.Add(new RegistrationQuestion() { QuestionText = "hi1?" });
q.Questions.Add(new RegistrationQuestion() { QuestionText = "hi2?" });
q.Questions.Add(new RegistrationQuestion() { QuestionText = "hi3?" });
return View(q);
}
And then in your view, you can have iteration in following way -
然后在您的视图中,您可以按以下方式进行迭代 -
@for (int i = 0; i < Model.Questions.Count; i++)
{
@Html.LabelFor(model => model.Questions[i].QuestionText, Model.Questions[i].QuestionText)
@Html.EditorFor(model => model.Questions[i].QuestionText)
<br/>
}
Output -
#2
0
I solved this by creating a partial view for the sub model and then dynamically inserting those (with JavaScript/jQuery) as needed.
我通过为子模型创建局部视图然后根据需要动态插入(使用JavaScript / jQuery)来解决这个问题。
#1
0
You can get it working like this. I made small changes to your model, instead of using ICollection<>, I used IList<> which have indexer support and can easily be iterated.
你可以让它像这样工作。我对你的模型做了一些小改动,而不是使用ICollection <>,我使用了IList <>,它有索引器支持,可以很容易地迭代。
public class QuestionSet
{
[Required]
public int Id { get; set; }
[Required]
public int ConferenceId { get; set; }
[Required]
public string Name { get; set; }
public virtual IList<RegistrationQuestion> Questions { get; set; }
}
public class RegistrationQuestion
{
[Required]
public int Id { get; set; }
[Required]
public int QuestionSetId { get; set; }
[Required]
public string QuestionText { get; set; }
}
I add following sample data in controller action -
我在控制器操作中添加以下示例数据 -
public ActionResult Index()
{
QuestionSet q = new QuestionSet();
q.Questions = new List<RegistrationQuestion>();
q.Questions.Add(new RegistrationQuestion() { QuestionText = "hi1?" });
q.Questions.Add(new RegistrationQuestion() { QuestionText = "hi2?" });
q.Questions.Add(new RegistrationQuestion() { QuestionText = "hi3?" });
return View(q);
}
And then in your view, you can have iteration in following way -
然后在您的视图中,您可以按以下方式进行迭代 -
@for (int i = 0; i < Model.Questions.Count; i++)
{
@Html.LabelFor(model => model.Questions[i].QuestionText, Model.Questions[i].QuestionText)
@Html.EditorFor(model => model.Questions[i].QuestionText)
<br/>
}
Output -
#2
0
I solved this by creating a partial view for the sub model and then dynamically inserting those (with JavaScript/jQuery) as needed.
我通过为子模型创建局部视图然后根据需要动态插入(使用JavaScript / jQuery)来解决这个问题。