C# 添加数据的局部视图

C# 添加数据的局部视图,c#,asp.net-mvc-4,partial-views,C#,Asp.net Mvc 4,Partial Views,所以在这里,我需要在一个视图中添加视频评论。 我有这样的主视图代码,显示视频和视频评论 <!-- language: C# --> @model AzureMediaPortal.ViewModels.ViewModelWatch @{ ViewBag.Title = "Watch"; } <div id="videoPlayer"> </div> <h2>@Html.DisplayFor(model => model.media.Ti

所以在这里,我需要在一个视图中添加视频评论。 我有这样的主视图代码,显示视频和视频评论

<!-- 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>
}
我需要传递来自部分视图的数据,并将其保存到包含媒体的数据库中。Id才能知道为视频插入的注释


非常感谢,将脚本放在局部视图通常是不好的做法。脚本被插入到视图的中间。为了保存部分视图中的信息,我使用ajax调用。为此,请在“发布”按钮中添加一个类

<input type="button" class="btnSubmit" value="Post" />
只需确保控制器上的输入与此处定义的名称完全匹配即可

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

我建议使用@Html.Action并从那里获取创建表单。此外,存储注释后,可以重定向到父操作。这将自动更新注释列表

但那是个坏主意


相反,您可以从ajax($.ajax)调用action来获取您的评论,并使用pure.js将旧的评论替换为新的评论列表。

您被困在哪里?我无法将部分视图中的数据插入数据库,也无法从部分视图中获取media.id
    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();
    }
<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);
 }