0

I have 2 models, Subjects and Grades

i want to create a dynamic view, with for each element in Subject, create a row in a table with subject name and 3 inputs for grades, and then fill the grades and passing to the controler for operations and storage on database.

i have this model

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; }

}

and this

public class Calificaciones
{

const double MIN_VALUE = 0.0;
const double MAX_VALUE = 5.0;

public int Id { get; set; }

[Required]
public int materiasId { get; set; }
public virtual Materias materia { get; set; }

[Required]
[Display(Name = "Nota 1")]
[Range(minimum: MIN_VALUE, maximum: MAX_VALUE, ErrorMessage = "Debe ingresar un numero entre 0.0  y 5.0")]
public Double nota1 { get; set; }

[Required]
[Display(Name = "Nota 2")]
[Range(minimum: MIN_VALUE, maximum: MAX_VALUE, ErrorMessage = "Debe ingresar un numero entre 0.0  y 5.0")]
public Double nota2 { get; set; }

[Required]
[Display(Name = "Nota 3")]
[Range(minimum: MIN_VALUE, maximum: MAX_VALUE, ErrorMessage = "Debe ingresar un numero entre 0.0  y 5.0")]
public Double nota3 { get; set; }

[Display(Name = "Resultado")]
public Double resultado { get; set; }


}

the controller is

// 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);
}

and the view

@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>

the view render:

I want to create multiple rows

i tried wit IEnumerable but it doesn't work any help?

kcrad100
  • 1
  • 2
  • The dupe explains why you cannot use a `foreach` loop for the collection, but I suspect you really want to be able to dynamically add (and remove) rows for your collection, in which case, refer [this answer](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) –  Mar 17 '17 at 21:16

1 Answers1

1

create class with one field a list of Calificaciones and use it as your model

  • i have create a viewmodel with a List of Calificaciones, and use it as @model but in the controller i dont get the value of Materias – kcrad100 Mar 17 '17 at 17:44