我正在制作一个项目,该项目是具有MVC和API的电影数据库。 用户必须能够对电影进行评论和评分。如果要显示电影,请使用名为MovieVM的Viewmodel,在其中可以查看电影的属性和已进行的注释。我在那里也有一个小的表格,用户可以在其中发表新评论。我可以单独发表评论吗?我需要发布整个viewmodel并再次提供信息吗?
@model MMDB_WebApp.Models.MovieVM
@{
string Id = null;
ViewData["Title"] = "Details";
if (TempData.ContainsKey("StateData"))
{
Dictionary<string, string> stateData = (Dictionary<string, string>)TempData["StateData"];
Id = stateData["_Id"];
}
TempData.Keep();
}
<div>
<h4>MovieVM</h4>
<hr />
@if (Model.Poster != null)
{
<img class="float-right" src="~/images/Posters/@Model.Poster" asp-append-version="true">
}
else
{
<img class="float-right" src="~/images/Posters/not_available.jpg">
}
<dl class="row">
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Title)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Title)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Description)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Description)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.ReleaseDate)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.ReleaseDate)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.DirectorId)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.DirectorId)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.DirectorName)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.DirectorName)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Actors)
</dt>
<dd class="col-sm-10">
<ul>
@foreach (var Actor in Model.Actors)
{
<li>
<img src="~/images/Avatars/no_avatar.png" />
<a asp-action="">@Actor.Name</a>
</li>
}
</ul>
</dd>
</dl>
<div class="row bootstrap snippets">
<div class="col-md-6 col-md-offset-2 col-sm-12">
<div class="comment-wrapper">
<div class="card card-info">
<div class="card-header">
Comment panel
</div>
<div class="card-body">
<form asp-action="comment" method="post">
<input hidden asp-for="NewComment.Date" value="@DateTime.Now" />
<input hidden asp-for="NewComment.MovieId" value="@Model.Id" />
<input hidden asp-for="NewComment.UserId" value="@Id" />
<textarea class="form-control" placeholder="write a comment..." rows="3" asp-for="NewComment.CommentText"></textarea>
<br>
<button type="submit" class="btn btn-info float-right">Post</button>
<div class="clearfix"></div>
<hr>
</form>
@if (Model.Comments.Count() != 0)
{
<ul class="media-list">
@foreach (var Comment in Model.Comments)
{
<li class="media">
@if (Comment.UserAvatar != null)
{
<img src="~/images/Avatars/@Comment.UserAvatar" class="img-circle avatar" alt="user profile image">
}
else
{
<img src="~/images/Avatars/no_avatar.png" class="img-circle avatar" alt="user profile image">
}
<div class="media-body">
<span class="text-muted float-right">
<small class="text-muted">@Comment.Date</small>
</span>
<strong class="text-success">@Comment.UserName</strong>
<p>
@Comment.CommentText
</p>
</div>
</li>
}
</ul>
}
else
{
<p>No comments yet</p>
}
</div>
</div>
</div>
</div>
</div>
</div>
public class MoviePutVM
{
[JsonPropertyName("id")]
public int Id { get; set; }
[JsonPropertyName("title")]
[Required(ErrorMessage = "title is required")]
public string Title { get; set; }
[JsonPropertyName("description")]
public string Description { get; set; }
[JsonPropertyName("releaseDate")]
public DateTime ReleaseDate { get; set; }
[JsonPropertyName("poster")]
[DisplayName("PosterName")]
public string Poster { get; set; }
[JsonPropertyName("posterFile")]
[DisplayName("UploadImage")]
public IFormFile PosterFile { get; set; }
[JsonPropertyName("directorId")]
public int? DirectorId { get; set; }
[JsonPropertyName("directorName")]
public string DirectorName { get; set; }
[JsonPropertyName("newComment")]
public CommentVM NewComment { get; set; }
[JsonPropertyName("actors")]
public virtual ICollection<ActorMovieVM> Actors { get; set; }
[JsonPropertyName("comments")]
public virtual ICollection<CommentVM> Comments { get; set; }
[JsonPropertyName("votes")]
public virtual ICollection<VotesVM> Votes { get; set; }
[JsonPropertyName("genres")]
public virtual ICollection<MovieGenresVM> Genres { get; set; }
}
public class CommentVM
{
[JsonPropertyName("id")]
public int Id { get; set; }
[JsonPropertyName("commentText")]
public string CommentText { get; set; }
[JsonPropertyName("date")]
public DateTime Date { get; set; }
//Relationships
[JsonPropertyName("userId")]
public string UserId { get; set; }
[JsonPropertyName("userName")]
public string UserName { get; set; }
[JsonPropertyName("userAvatar")]
public string UserAvatar { get; set; }
[JsonPropertyName("movieId")]
public int? MovieId { get; set; }
}
答案 0 :(得分:1)
我可以单独发表评论吗?我需要发布整个viewmodel吗 并再次提供信息?
您可以按值将注释模型单独传递给控制器,您不需要传递整个视图模型,甚至甚至不需要NewComment字段。
您只需要确保提交表单中相应字段的 name属性与您的评论模型一致, 将所需的信息传递给评论,并将其存储在评论表中。
<form asp-action="comment" method="post">
<input hidden name="Date" value="@DateTime.Now" />
<input hidden name="MovieId" value="@Model.Id" />
<input hidden name="UserId" value="@Id" />
<textarea name="CommentText" class="form-control" placeholder="write a comment..." rows="3" ></textarea>
<br>
<button type="submit" class="btn btn-info float-right">Post</button>
<div class="clearfix"></div>
<hr>
</form>
控制器:
[HttpPost]
public IActionResult comment(CommentVM newcomment)
{
//save this new comment to the comment table which related to the movie table.
_context.CommentVM.Add(newcomment);
_context.SaveChanges();
return RedirectToAction("Index");
}
这是测试结果: