Map Id of one model to another without a Dropdownlist

Asked

Viewed 231 times

0

I had a problem making relationships in my application. my project is from a school, and in it I have to have the occurrences. But when it comes to relating the Student to the Occurrence happened to me that, in the view create of the occurrences, I see a dropdownlist with the registered students, so that I do the relationship, ie, Mask the occurrence in the student, I mean, what the student did. What I really wanted was that when I uploaded the student details, I would have a Collapse(accordion type) where the user could put the occurrence for that specific student, taking his ID, ie, passing the student id in the detail view and the same student id for the accurate mapping of the occurrence. That is, register the occurrence for that student, and that it is not loaded a dropdaownlist with the students.

I’ll put here, the student registration view to see what can be done:

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

    <div class="form-horizontal">
    <h4>Ocorrencia</h4>
    <hr />
    @Html.ValidationSummary(true)

    <div class="form-group">
        @Html.LabelFor(model => model.Tipo, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Tipo)
            @Html.ValidationMessageFor(model => model.Tipo)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Causa, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Causa)
            @Html.ValidationMessageFor(model => model.Causa)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Observacao, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Observacao)
            @Html.ValidationMessageFor(model => model.Observacao)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.AlunoId, "AlunoId", new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("AlunoId", String.Empty)
            @Html.ValidationMessageFor(model => model.AlunoId)
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Salvar" class="btn btn-default" />
        </div>
    </div>
</div>
}

Here’s the part of the controller that populates the dropdownlist:

     public ActionResult Index()
    {
        var ocorrencias = db.Ocorrencias.Include(o => o.Aluno);
        return View(ocorrencias.ToList());
    }

    // GET: /Ocorrencias/Detalhes/5
    public ActionResult Detalhes(long? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Ocorrencia ocorrencia = db.Ocorrencias.Find(id);
        if (ocorrencia == null)
        {
            return HttpNotFound();
        }
        return View(ocorrencia);
    }

    // GET: /Ocorrencias/Adiciona
    public ActionResult Adiciona()
    {
        ViewBag.AlunoId = new SelectList(db.Alunos, "Id", "Nome");
        return View();
    }

    // POST: /Ocorrencias/Adiciona
    // 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 Adiciona([Bind(Include="Id,Tipo,Causa,Observacao,AlunoId")]    Ocorrencia ocorrencia)
    {
        if (ModelState.IsValid)
        {
            db.Ocorrencias.Add(ocorrencia);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.AlunoId = new SelectList(db.Alunos, "Id", "Nome", ocorrencia.AlunoId);
        return View(ocorrencia);
    }

    // GET: /Ocorrencias/Edita/5
    public ActionResult Edita(long? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Ocorrencia ocorrencia = db.Ocorrencias.Find(id);
        if (ocorrencia == null)
        {
            return HttpNotFound();
        }
        ViewBag.AlunoId = new SelectList(db.Alunos, "Id", "Nome", ocorrencia.AlunoId);
        return View(ocorrencia);
    }

    // POST: /Ocorrencias/Edita/5
    // 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 Edita([Bind(Include="Id,Tipo,Causa,Observacao,AlunoId")] Ocorrencia ocorrencia)
    {
        if (ModelState.IsValid)
        {
            db.Entry(ocorrencia).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        ViewBag.AlunoId = new SelectList(db.Alunos, "Id", "Nome", ocorrencia.AlunoId);
        return View(ocorrencia);
    }

    // GET: /Ocorrencias/Remove/5
    public ActionResult Remove(long? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Ocorrencia ocorrencia = db.Ocorrencias.Find(id);
        if (ocorrencia == null)
        {
            return HttpNotFound();
        }
        return View(ocorrencia);
    }

    // POST: /Ocorrencias/Remove/5
    [HttpPost, ActionName("Remove")]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteConfirmed(long id)
    {
        Ocorrencia ocorrencia = db.Ocorrencias.Find(id);
        db.Ocorrencias.Remove(ocorrencia);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

And here’s what I put on the Students model, which makes the load of students' names appear on the dropdownlist:

    public ICollection<Ocorrencia> Ocorrencias { get; set; }
    public Aluno()
    {
        this.Ocorrencias = new HashSet<Ocorrencia>();
    }

1 answer

3


Just change:

@Html.DropDownList("AlunoId", String.Empty)

For:

@Html.HiddenFor(model => model.AlunoId)
<div>@model.Aluno.Nome</div>

Controller

public ActionResult AdicionarOcorrencia(int id)
{
    var aluno = db.Alunos.SingleOrDefault(a => a.Id == id);
    return View(aluno);
}
  • And in my controller and Student model ? Would I change the terms as well ? I’ll edit the questions and put what’s in the controller and model, which I think is why I put that list !

  • You don’t need to change anything on Model, only that his Controller will have to initialize the Aluno. I’ll update the answer.

  • And Icollection in the Student model, is there a problem ? No ?

  • The Occurrences? No. Use normal.

  • No, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no. Speaking of the student, I would exchange the Icollection for Occurrence Id and Occurrence ?

  • Then you’d have to use the Html.BeginCollectionItem to iterate the collection of Occurrences, as I told you before. Then I think it is worth opening another question.

  • It’s because I didn’t want to dropdown, I wanted the incident to be linked to the student. From what I understand, when I do an icollection, I make a list that can be populated in a dropdown. I didn’t want this, I wanted to link the occurrence to the student, I mean the student’s ID. Then it would be a relation of 1:N. That is, one student for several occurrences.

  • So, like I said, open up a new question that I teach you to ask.

  • A question about this relationship ? I mean, I’m lost on how to open a new question. I’ll do one with the title of how to use Html.Begincollectionitem, OK ? It can be ?

  • Here gypsy the link to the question I just asked link

  • Gypsy, I’m going to need a little help in the controller. I tried to do this implementation you indicated, but it didn’t work out wrong. Would it be in that question, or the other ?

  • Put the error in the other, please.

  • I’ll put the whole Controller can be ?

  • Quiet. You can put.

  • Okay, put there !

Show 10 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.