2014-03-07 2 views
0

여기에서 비디오에 대한 주석을 하나의보기에 추가해야합니다. 나는이 비디오에 대한 비디오 및 설명을 표시하는 것과 같은 기본보기 코드를 가지고 있습니다. 데이터를 추가 할 부분 뷰

<!-- 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") 
} 

이 내 뷰 모델

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(); 
    } 

인 그것을 데이터베이스에 저장하십시오. 미디어를 포함 시키십시오. 비디오에 삽입 된 주석을 알고 싶습니다.

감사합니다. muchhh

+0

어디서 붙어 있습니까? – user13500

+0

부분보기의 데이터를 데이터베이스에 삽입 할 수없고 부분보기에서 media.id를 가져 오는 방법 – veinvein

답변

0

퍼팅 스크립트를 부분적으로 보는 것은 일반적으로 좋지 않습니다. 스크립트가보기의 중간에 삽입됩니다. 부분 뷰에서 정보를 저장하기 위해 저는 아약스 호출을 사용합니다. 그 메인 페이지에서 스크립트에

<input type="button" class="btnSubmit" value="Post" /> 

다음 포스트 버튼에 클래스를 추가 수행

$(document).on('click', '.btnSubmit', function(){ 
    $.ajax({ 
     url: '@Url.Action("Action", "Controller")', 
     cache: false, 
     async: true, 
     data: { 
      //put the data that you want to save from the partial here 
      id: $('#hiddenID').val(), 
      content: $('#Content').val() 
     }, 
     success: function (_result) { 
      //can add something here like an alert, close a popup something along those lines 
     } 
    }); 
}); 

단지 확인 컨트롤러 일치에 입력하면

여기에 정의 정확히 이름
[HttpPost] 
public ActionResult Action(int id, string content){ 
    //database call to save the fields 
    //see here for an example of returning json http://stackoverflow.com/questions/1482034/extjs-how-to-return-json-success-w-data-using-asp-net-mvc 
    return Json(json); 
} 
0

@ Html.Action을 사용하여 거기에서 작성 양식을 가져 오는 것이 좋습니다. 또한 댓글이 저장되면 상위 작업으로 리디렉션 할 수 있습니다. 그러면 자동으로 댓글 목록이 업데이트됩니다.

하지만 그건 좋은 생각이 아닙니다.

아약스 ($ .ajax)에서 action을 호출하고 pure.js를 사용하여 이전 코멘트를 새 코멘트 목록으로 바꿀 수 있습니다.