C# 如何修改create方法?
我有这个模型:C# 如何修改create方法?,c#,asp.net-mvc,asp.net-mvc-4,model,C#,Asp.net Mvc,Asp.net Mvc 4,Model,我有这个模型: public class UsuarioMusica { public int UsuarioMusicaId { get; set; } public int UserId { get; set; } public int MusicId {get; set;} } 我将生成一个控制器和视图,但我将只讨论create方法。 我的问题是,我需要从登录用户和“MusicId”中获取用户ID,以便在表中建立关系,对吗?因此,我在music controll
public class UsuarioMusica
{
public int UsuarioMusicaId { get; set; }
public int UserId { get; set; }
public int MusicId {get; set;}
}
我将生成一个控制器和视图,但我将只讨论create方法。
我的问题是,我需要从登录用户和“MusicId”中获取用户ID,以便在表中建立关系,对吗?因此,我在music controller中创建了一个搜索视图和搜索方法,列出保存在music model中的所有音乐,搜索视图:
@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>
这里的主要困难是如何修改create方法,从用户在“将音乐添加到我的收藏”链接上创建的行中的音乐中获取用户ID musicID,并保存到UsuarioMusica表。
@Html。ActionLink
是对GET方法的重定向。您需要为发布项目ID的每一行包含一个
元素。然而,这意味着每次发布时都要重新创建视图,因此使用ajax将大大提高性能
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(m => item.genero.Nome)
</td>
......
<td>
<button type="button" class="addmusic" data-id="@item.MusicaId">Add</button>
</td>
}
注意:要从DOM中删除items行(不仅仅是按钮),请使用
和控制器
[HttpPost]
public JsonResult Create(int musicaID)
{
var userID = // you should know this on the server
// call a service that saves the userID and musicaID to the database
var usuariomusica = new UsuarioMusica()
{
UserId = userID,
MusicId = musicaID
};
db.UsuarioMusicas.Add(usuariomusica);
db.SaveChanges();
return Json(true);
}
注意:如果save方法失败,那么使用返回Json(null)
这样脚本就会被告知失败。还请注意缺少[ValidateAntiForgeryToken]
。如果您希望这样做,那么还需要在ajax请求中传递令牌,如下所示
var token = $('input[name="__RequestVerificationToken"]').val();
$.post(url, { musicaID: id, __RequestVerificationToken: token }, function(data) {
您的视图甚至没有显示任何用于发回
Create()
方法的
元素,而且由于您已经知道控制器中的用户ID
,因此该方法可能只需要具有参数int musicID
。你也应该考虑使用Ajax,这样用户可以停留在同一个页面上,并且可以继续向他们的集合中添加新的音乐项,而不必不断地重定向到相同的视图。@ StpMeMeCek我可以在搜索视图中放一个<代码> <代码>,用户ID不是问题,我知道如何去解决它,我的问题是,如果我将@Html.ActionLink(“将音乐添加到我的收藏”、“创建”、“UsuarioMusica”})
放在搜索视图中,我会得到一个错误,因为我在同一视图中有两个模型。@StephenMuecke首先我需要解决这个问题,然后,我需要解决如何修改create方法以添加数据库为什么在同一视图中有两个模型?它可以是@Html.ActionLink(“将音乐添加到我的收藏”、“创建”、“UsuarioMusica”、new{id=item.id})
将id传递给Create方法,但是ActionLink
是GET,而不是POST。您需要一个
元素,并为项添加一个路由值。ID
因此这是一篇文章,但我建议您为此使用ajax。@StephenMuecke,因为在第一行视图中,我调用一个模型@model IEnumerable
,在actionLink上,我调用另一个模型UsuarioMusica
,明白吗?我从来没有使用过Ajax,你有什么例子给我吗?谢谢Stephen,我正在尝试实现Ajax脚本,但我在上一个}
中遇到了语法错误,我正在尝试解决,不在乎这个,你已经在帮我了,我刚刚将上面第二个代码片段中的脚本剪切粘贴到一个视图中,没有“语法”错误,所以不确定你可能做错了什么我没有一个服务可以将userId/MusicaID保存在数据库中,如果你看我的问题,我将我的create方法放在第行db.UsuarioMusicas.Add中(通常音乐);
该方法将在数据库中添加usuariomusica,我需要创建另一个方法来传递变量musicaID和userID以添加到数据库中usuariomusica
是一个包含属性userID
和MusicId
的类,因此只需初始化并创建usuariomusica
的新实例,设置属性并使用Add
方法保存它-var x=new UsuarioMusica(){UserId=UserId,MusicId=musicaID};db.UsuarioMusicas.Add(x);
谢谢@Stephen,它正在工作,我只需要调整表的布局,并在用户单击Add按钮时实现删除行的功能
if(data) {
var row = self.closest('tr')
row.remove();
}
[HttpPost]
public JsonResult Create(int musicaID)
{
var userID = // you should know this on the server
// call a service that saves the userID and musicaID to the database
var usuariomusica = new UsuarioMusica()
{
UserId = userID,
MusicId = musicaID
};
db.UsuarioMusicas.Add(usuariomusica);
db.SaveChanges();
return Json(true);
}
var token = $('input[name="__RequestVerificationToken"]').val();
$.post(url, { musicaID: id, __RequestVerificationToken: token }, function(data) {