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

0

I have the following view where I have a list of articles registered in the database, there 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 the project image

Viewactionactioncode

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

        return View(avaliarArtigo);
    }
    
asked by anonymous 01.11.2018 / 03:14

1 answer

0

Simply pass the article id by clicking Evaluate:

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

Action Evaluate

In your Action Evaluate you will receive id of the article and instantiate a new object of type AvaliarArtigo . The ideal is that your AvaliarArtigo class has a ArtigoId attribute, to know which article you rated, I suggest you look like this:

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

and your Action Avaliar will look like this:

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

     return View(avaliarArtigo);
 }

Action that saves article rating

And in your action to save the evaluation of the article, just receive the type AvaliarArtigo and save it:

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

      return View(avaliarArtigo);
 }
    
29.11.2018 / 18:33