i have this model:
public class UsuarioMusica
{
public int UsuarioMusicaId { get; set; }
public int UserId { get; set; }
public int MusicId {get; set;}
}
And i generate a controller and view, but I'll talk only about the create method. My problem is, I need to take the UserId from logged user and "MusicId" to do a relation at table, right? So, I created a search view and search method in music controller, that list all music saved at music model, search view:
@model IEnumerable<TestTcc2.Models.Musica>
@{
ViewBag.Title = "Search";
Layout = "~/Views/Shared/_LayoutOuvinte.cshtml";
}
<h2>Busca de Música</h2>
<table class="table table-hover table-bordered">
<tr>
<th>
<span>Genero</span>
</th>
<th>
<span>Nome</span>
</th>
<th>
<span>Artista</span>
</th>
<th>
<span>Preço</span>
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.genero.Nome)
</td>
<td>
@Html.DisplayFor(modelItem => item.Nome)
</td>
<td>
@Html.DisplayFor(modelItem => item.NomeArtista)
</td>
<td>
@Html.DisplayFor(modelItem => item.Preco)
</td>
<td>
@Html.ActionLink("Play", "", new { path = item.path }) |
</td>
</tr>
}
</table>
My Idea is, in this view, I'll create a new link for each row like @Html.ActionLink("Add music to my collection", "Create", "UsuarioMusica" }) that will call the create method and will add to usuarioMusica model the userId from logged user and musicId from music listed at table.
But I don't have idea how to do this on create method at UsuarioMusica controller, actually, this is my create method:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(UsuarioMusica usuariomusica)
{
if (ModelState.IsValid)
{
db.UsuarioMusicas.Add(usuariomusica);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(usuariomusica);
}
The principal dificulty here, is how to modify the create method to take the userID, musicID from music in row that user create on link "Add music to my collection" and save at UsuarioMusica table.
<form>elements for posting back to theCreate()method, and since you already know theUserIdin the controller, the method probably just needs to have parameterint musicID. You should also consider using ajax for this so the user stays on the same page and can continue to add new music items to their collection without constantly redirecting back to the same view.<form>in search view and UserId is not a problem, i know how to take it, my problem is, if I put the@Html.ActionLink("Add music to my collection", "Create", "UsuarioMusica" })in the search view, I'll get a error, because i have two models on the same view.@Html.ActionLink("Add music to my collection", "Create", "UsuarioMusica", new { id = item.ID })to pass the ID to the create method, butActionLinkis a GET, not a POST. You need a<form>element and add a route value for theitem.IDso its a POST, but I recommend you use ajax for this.@model IEnumerable<TestTcc2.Models.Musica>and on actionLink i'm calling another modelUsuarioMusica, understand? I never use Ajax, do you have any example to send me?