Pass id value to from one view to another - Asp.Net MVC

Asked

Viewed 606 times

0

I have the following view where I have a list of articles registered in the database, then I have the evaluate button that redirects to another view where it is in another controller, I want to pass id of the selected article to another controller. Follow image of the projectview onde lista os artigos ja cadastrados

View da avaliação do artigo

View action code evaluate

    public ActionResult Create([Bind(Include = "AvaliacaoID,NotaArtigo,ComentarioRevisao")] AvaliarArtigo avaliarArtigo)
    {
        if (ModelState.IsValid)
        {
            db.AvaliarArtigos.Add(avaliarArtigo);
            db.SaveChanges();
            return RedirectToAction("Index", "Home");
        }

        return View(avaliarArtigo);
    }

1 answer

0


Just pass the article id when clicking evaluate:

@Html.ActionLink("Avaliar", "NomeAction", "NomeController", new { id = item.id})

Action Avaliar

In your action Evaluate you will receive the id of the article and instantiating a new object of the type AvaliarArtigo. The ideal is that your class AvaliarArtigo have an attribute ArtigoId, to find out which article you evaluated, I suggest it stay that way:

 public class AvaliarArtigo
 {
     public int AvaliacaoID { get; set; }
     public float NotaArtigo { get; set; }
     public string ComentarioRevisao { get; set; }
     public int ArtigoId { get; set; }
 }

and its Action Avaliar will look like this:

 public ActionResult Avaliar(int id)
 {
     avaliarArtigo = new AvaliarArtigo();
     avaliarArtigo.ArtigoId = id;

     return View(avaliarArtigo);
 }

Action that saves article evaluation

And in your save article evaluation action, just receive the type AvaliarArtigo and save him:

 public ActionResult Save(AvaliarArtigo avaliarArtigo)
 {
     if (ModelState.IsValid)
     {
         db.AvaliarArtigos.Add(avaliarArtigo);
         db.SaveChanges();
         return RedirectToAction("Index", "Home");
      }

      return View(avaliarArtigo);
 }

Browser other questions tagged

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