如何从 MVC 5 中的模型读取 ICollection
How to read ICollection from Model in MVC 5
我需要从视图中的模型读取 Icollection。我有这种情况。
我有 3 个 classes:Alternative、Question 和 QuestionAlternative
Class
namespace Models
{
public class QuestionAlternative
{
public int QuestionAlternativeID { get; set; }
public int QuestionID { get; set; }
public int AlternativeID { get; set; }
public virtual Question Question { get; set; }
public virtual ICollection<Alternative> Alternative { get; set; }
}
}
问题
namespace Models
{
public class Question
{
public int QuestionID { get; set; }
public int FactorID { get; set; }
public string Description { get; set; }
public string Complement { get; set; }
public int IndexOrder { get; set; }
public double Weight { get; set; }
public virtual Factor Factor { get; set; }
}
}
备选
namespace Models
{
public class Alternative
{
public int AlternativeID { get; set; }
public string Description { get; set; }
public double Weight { get; set; }
public int IndexOrder { get; set; }
}
}
我的控制器
namespace Inventario.Controllers
{
public class HomeController : Controller
{
private InventarioContext db = new InventarioContext();
public ActionResult Index()
{
return View();
}
public ActionResult Questionnaire()
{
var questionAlternative = db.QuestionAlternatives.Include(qa => qa.Question).Include(qa => qa.Alternative);
return View("../Equipment/Questionnaire", questionAlternative);
}
}
}
我的观点
<div class="table-responsive">
@using (Html.BeginForm("getRadioValues", "Equipment", FormMethod.Post))
{
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>Nº</th>
</tr>
</thead>
<tbody>
@foreach (var question in Model.GroupBy(q=>q.Question))
{
<tr>
<td width="10px"><b>@question.Key.QuestionID - @question.Key.Description</b> </td>
</tr>
foreach (var alternative in question)
{
<tr>
<td>
@Html.RadioButton("radio" + @question.Key.QuestionID, alternative.QuestionAlternativeID, false)@alternative.Alternative.HOW TO ACCESS THE FIELDS OS <Alternatives> LIST
</td>
</tr>
}
}
</tbody>
</table>
<input id="submit" type="submit" value="submit" />
}
</div>
在我看来,我将每个问题的所有备选方案分组。我需要访问 Alternative class 的属性来打印备选方案,但它是我模型中的一个集合。怎么做?如何访问 public virtual ICollection Alternative { get;放; } 内部模型
您实际上完全按照您写的方式访问它:@alternative.Alternative
。它是一个集合(你说得对),所以你可以通过索引访问或枚举它。+
我需要从视图中的模型读取 Icollection。我有这种情况。
我有 3 个 classes:Alternative、Question 和 QuestionAlternative
Class
namespace Models
{
public class QuestionAlternative
{
public int QuestionAlternativeID { get; set; }
public int QuestionID { get; set; }
public int AlternativeID { get; set; }
public virtual Question Question { get; set; }
public virtual ICollection<Alternative> Alternative { get; set; }
}
}
问题
namespace Models
{
public class Question
{
public int QuestionID { get; set; }
public int FactorID { get; set; }
public string Description { get; set; }
public string Complement { get; set; }
public int IndexOrder { get; set; }
public double Weight { get; set; }
public virtual Factor Factor { get; set; }
}
}
备选
namespace Models
{
public class Alternative
{
public int AlternativeID { get; set; }
public string Description { get; set; }
public double Weight { get; set; }
public int IndexOrder { get; set; }
}
}
我的控制器
namespace Inventario.Controllers
{
public class HomeController : Controller
{
private InventarioContext db = new InventarioContext();
public ActionResult Index()
{
return View();
}
public ActionResult Questionnaire()
{
var questionAlternative = db.QuestionAlternatives.Include(qa => qa.Question).Include(qa => qa.Alternative);
return View("../Equipment/Questionnaire", questionAlternative);
}
}
}
我的观点
<div class="table-responsive">
@using (Html.BeginForm("getRadioValues", "Equipment", FormMethod.Post))
{
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>Nº</th>
</tr>
</thead>
<tbody>
@foreach (var question in Model.GroupBy(q=>q.Question))
{
<tr>
<td width="10px"><b>@question.Key.QuestionID - @question.Key.Description</b> </td>
</tr>
foreach (var alternative in question)
{
<tr>
<td>
@Html.RadioButton("radio" + @question.Key.QuestionID, alternative.QuestionAlternativeID, false)@alternative.Alternative.HOW TO ACCESS THE FIELDS OS <Alternatives> LIST
</td>
</tr>
}
}
</tbody>
</table>
<input id="submit" type="submit" value="submit" />
}
</div>
在我看来,我将每个问题的所有备选方案分组。我需要访问 Alternative class 的属性来打印备选方案,但它是我模型中的一个集合。怎么做?如何访问 public virtual ICollection Alternative { get;放; } 内部模型
您实际上完全按照您写的方式访问它:@alternative.Alternative
。它是一个集合(你说得对),所以你可以通过索引访问或枚举它。+