所以在這裏,我需要在一個視圖中添加對視頻的評論。 我有這樣的主視圖代碼,顯示該視頻的視頻和評論。部分視圖來添加數據
<!-- language: C# -->
@model AzureMediaPortal.ViewModels.ViewModelWatch
@{
ViewBag.Title = "Watch";
}
<div id="videoPlayer">
</div>
<h2>@Html.DisplayFor(model => model.media.Title)</h2>
<h3> By @Html.DisplayFor(model => model.media.UserId) at @Html.DisplayFor(model => model.media.UploadDate) </h3>
@Html.HiddenFor(model => model.media.Id)
@Html.HiddenFor(model => model.media.AssetId)
@Html.HiddenFor(model => model.media.FileUrl, new { id = "fileUrl" })
<div class="display-label" style="font-weight:bold">
@Html.DisplayNameFor(model => model.media.Description)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.media.Description)
</div>
<br />
<div class="display-label" style="font-weight:bold">
@Html.DisplayName("Category")
</div>
<div class="display-field">
@Html.DisplayFor(model => model.media.Category.CategoryName)
</div>
<h3>Comments</h3>
@foreach (var item in Model.comment)
{
<div class="display-label" style="font-weight:bold">
@item.UserId
</div>
<div class="display-field">
@item.Content
</div>
}
@Html.Partial("Post",new AzureMediaPortal.ViewModels.ViewModelWatch())
@section Scripts {
<script src="~/Scripts/playerframework.min.js"></script>
<script src="~/Scripts/media-player.js"></script>
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
mediaPlayer.initFunction("videoPlayer", $("#fileUrl").val());
</script>
}
和這個局部視圖
@model AzureMediaPortal.ViewModels.ViewModelWatch
@{
ViewBag.Title = "Post";
}
<h2>Add Comment</h2>
@Html.HiddenFor(model => model.cmnt.MediaElement.Id)
@using (Html.BeginForm("Post","Home",FormMethod.Post)) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Add Comment</legend>
<div class="editor-label" style="font-weight:bold">
@Context.User.Identity.Name
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.cmnt.Content)
@Html.ValidationMessageFor(model => model.cmnt.Content)
</div>
<p>
<input type="submit" value="Post" />
</p>
</fieldset>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
這是我的ViewModel
public class ViewModelWatch
{
public MediaElement media { get; set; }
public List<Comment> comment { get; set; }
public Comment cmnt { get; set; }
}
,這是我的控制器
public ActionResult Watch(int id)
{
ViewModelWatch vm = new ViewModelWatch();
vm.media = _repository.GetMedia(id);
vm.comment = _repository.GetMediaComment(id);
return View(vm);
}
public ActionResult Post()
{
return View();
}
[HttpPost]
public ActionResult Post(Comment comment, int id)
{
if (ModelState.IsValid)
{
comment.UserId = User.Identity.Name;
comment.MediaElement.Id = id;
db.Comments.Add(comment);
db.SaveChanges();
return RedirectToAction("Watch");
}
return View();
}
我需要通過從局部視圖數據並將其保存到數據庫包括media.Id知道爲視頻插入的評論。
感謝這麼muchhh
你在哪裏卡住了? – user13500
我無法將數據從我的部分視圖插入到數據庫以及如何從部分視圖中獲取media.id – veinvein