【问题标题】:How to read ICollection from Model in MVC 5如何从 MVC 5 中的模型中读取 ICollection
【发布时间】:2015-06-01 04:11:07
【问题描述】:

我需要从 Model in View 中读取 Icollection。我有这种情况。

我有 3 个类:Alternative、Question 和 QuestionAlternative

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 类的属性来打印替代品,但它是我模型中的一个集合。怎么做?如何访问公共虚拟 ICollection Alternative { get;放; } 内部模型

【问题讨论】:

  • 您创建的单选按钮与您的模型没有关系,所以这不会发回给您的模型!您需要重新考虑这一点并创建视图模型以显示/编辑您需要的内容并在回发时绑定到模型。

标签: c# linq entity-framework asp.net-mvc-5 tuples


【解决方案1】:

您实际上是按照您所写的方式访问它:@alternative.Alternative。它是一个集合(你提到的正确),所以你可以通过索引访问或通过它枚举。+

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 2018-01-03
    • 1970-01-01
    相关资源
    最近更新 更多