C# 使用多模型C MVC RAZOR从视图中传递和获取值

C# 使用多模型C MVC RAZOR从视图中传递和获取值,c#,asp.net-mvc,razor,entity-framework-6,C#,Asp.net Mvc,Razor,Entity Framework 6,我有两个模型,科目和等级 我想创建一个动态视图,对于Subject中的每个元素,在一个表中创建一行,其中包含Subject名称和3个等级输入,然后填充等级并传递给controller,以便对数据库进行操作和存储 我有这个型号 namespace CalculaNotas.Models public class Materias { public int materiasId { get; set; } [Required] [MinLength(length: 5, ErrorMessage

我有两个模型,科目和等级

我想创建一个动态视图,对于Subject中的每个元素,在一个表中创建一行,其中包含Subject名称和3个等级输入,然后填充等级并传递给controller,以便对数据库进行操作和存储

我有这个型号

namespace CalculaNotas.Models
public class Materias
{

public int materiasId { get; set; }

[Required]
[MinLength(length: 5, ErrorMessage = "El nombre debe contener al menos 5 caracteres")]
[MaxLength(length: 20, ErrorMessage = "El nombre debe contener maximo 2 caracteres")]
[Index(IsUnique = true)]
public string nombre { get; set; }

public virtual List<Calificaciones> calificaciones { get; set; }

}
控制器是

// GET: Calificaciones/Create
public ActionResult Create()
{
    var materias = db.Materias.ToList();
    ViewBag.materias = materias;
    return View();
}

// POST: Calificaciones/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,materiasId,nota1,nota2,nota3,resultado")] Calificaciones calificaciones)
{
    if (ModelState.IsValid)
    {
        db.Calificaciones.Add(calificaciones);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    ViewBag.materiasId = new SelectList(db.Materias, "materiasId", "nombre", calificaciones.materiasId);
    return View(calificaciones);
}
那景色呢

@model  CalculaNotas.Models.Calificaciones
@{
ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm())
{
 @Html.AntiForgeryToken()

<div class="form-horizontal">
 <h4>Calificaciones</h4>

<table class="table table-bordered">
    <tr >
        <th class="col-md-1"> @Html.DisplayNameFor(model => model.materia) </th>
        <th class="col-md-1" style="text-align:center">@Html.DisplayNameFor(model => model.nota1)</th>
        <th class="col-md-1" style="text-align:center">@Html.DisplayNameFor(model => model.nota2)</th>
        <th class="col-md-1" style="text-align:center">@Html.DisplayNameFor(model => model.nota3)</th>
    </tr>

    @foreach (var materia in (IEnumerable<CalculaNotas.Models.Materias>)ViewBag.materias)
    {
        <tr >
            <td class="col-md-1">@Html.DisplayFor(model => materia.nombre)</td>
            <td class="col-md-1">
                @Html.EditorFor(model => model.nota1, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.nota1, "", new { @class = "text-danger" })
            </td>
            <td class="col-md-1">
                @Html.EditorFor(model => model.nota2, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.nota2, "", new { @class = "text-danger" })
            </td>
            <td class="col-md-1">
                @Html.EditorFor(model => model.nota3, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.nota3, "", new { @class = "text-danger" })
            </td>
视图渲染:


我尝试使用IEnumerable,但它没有任何帮助?

用一个字段创建一个类一个Calificaciones列表,并将其用作您的模型

我创建了一个带有Calificaciones列表的viewmodel,并将其用作@model,但在控制器中,我没有得到Material的值。dupe解释了为什么不能对集合使用foreach循环,但我怀疑您确实希望能够动态地添加和删除集合的行,在这种情况下,请参阅
@model  CalculaNotas.Models.Calificaciones
@{
ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm())
{
 @Html.AntiForgeryToken()

<div class="form-horizontal">
 <h4>Calificaciones</h4>

<table class="table table-bordered">
    <tr >
        <th class="col-md-1"> @Html.DisplayNameFor(model => model.materia) </th>
        <th class="col-md-1" style="text-align:center">@Html.DisplayNameFor(model => model.nota1)</th>
        <th class="col-md-1" style="text-align:center">@Html.DisplayNameFor(model => model.nota2)</th>
        <th class="col-md-1" style="text-align:center">@Html.DisplayNameFor(model => model.nota3)</th>
    </tr>

    @foreach (var materia in (IEnumerable<CalculaNotas.Models.Materias>)ViewBag.materias)
    {
        <tr >
            <td class="col-md-1">@Html.DisplayFor(model => materia.nombre)</td>
            <td class="col-md-1">
                @Html.EditorFor(model => model.nota1, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.nota1, "", new { @class = "text-danger" })
            </td>
            <td class="col-md-1">
                @Html.EditorFor(model => model.nota2, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.nota2, "", new { @class = "text-danger" })
            </td>
            <td class="col-md-1">
                @Html.EditorFor(model => model.nota3, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.nota3, "", new { @class = "text-danger" })
            </td>