使用部分视图更新asp.net ajax

时间:2015-02-08 02:09:36

标签: c# asp.net ajax asp.net-mvc-4

尝试在部分视图中仅显示文本框中的一条评论。

要获取一些数据,您需要会话控制器:

private ConferenceContext context = new ConferenceContext();

        //
        // GET: /Session/

        public ActionResult Index()
        {
            ConferenceContext context = new ConferenceContext();
            List<Session> sessions = context.Sessions.ToList();
            return View(sessions);
        }
//
        // GET: /Session/Details/5

        public ActionResult Details(int id = 0)
        {
            Session session = context.Sessions.Find(id);
            if (session == null)
            {
                return HttpNotFound();
            }
            return View(session);
        }

详细信息在会话文件夹中查看:

    @model Conference.Models.Session

<h3>
    @Model.Title
</h3>
<div>
    @Model.Abstract
</div>

@Html.Action("_GetForSession", "Comment", new { SessionID = Model.SessionID })

然后是CommentController,它使用局部视图_GetForSession来显示文本框中的文本:

ConferenceContext context = new ConferenceContext();
        public PartialViewResult _GetForSession(Int32 sessionID)
        {
            ViewBag.SessionID = sessionID;
            List<Comment> comments = context.Comments.Where(c => c.SessionID == sessionID).ToList();
            return PartialView("_GetForSession", comments);
        }

        [ChildActionOnly()]
        public PartialViewResult _CommentForm(Int32 sessionID)
        {
            Comment comment = new Comment() { SessionID = sessionID };
            return PartialView("_CommentForm", comment);
        }

        [ValidateAntiForgeryToken()]
        public PartialViewResult _Submit(Comment comment)
        {
            context.Comments.Add(comment);
            context.SaveChanges();

            List<Comment> comments = context.Comments.Where(c => c.SessionID == comment.SessionID).ToList();
            ViewBag.SessionID = comment.SessionID;

            return PartialView("_GetForSession", comments);
        }

以下是Comment文件夹中的_GetForSession视图:

@model IEnumerable<Conference.Models.Comment>

<div id="comments">
    <ul>
        @foreach (var comment in Model)
        {
            <li>@comment.Content</li>
        }
    </ul>

    @using(Ajax.BeginForm("_Submit", "Comment", new AjaxOptions() { UpdateTargetId = "comments" }))
    {
        @Html.AntiForgeryToken();
        @Html.Action("_CommentForm", new { SessionID = ViewBag.SessionID })
    }
</div>

_GetForSession从Comment文件夹中的_CommentForm获取数据:

@model Conference.Models.Comment

@Html.HiddenFor(m => m.SessionID)

<div>
    @Html.LabelFor(m => m.Content)
    @Html.EditorFor(m => m.Content)
</div>
<button type="submit">Submit Comment</button>

现在主要的Context将来自Models中的ConferenceContext:

public class ConferenceContext : DbContext
    {
        public DbSet<Session> Sessions { get; set; }
        public DbSet<Speaker> Speakers { get; set; }
        public DbSet<Comment> Comments { get; set; }
    }

来自ConferenceContextInitializer的Context本身:

public class ConferenceContextInitializer : DropCreateDatabaseAlways<ConferenceContext>
    {
        protected override void Seed(ConferenceContext context)
        {
            context.Sessions.Add(
                new Session()
                {
                    Title = "Partial View",
                    Abstract = "View Within the Main",
                    Speaker = context.Speakers.Add(new Speaker()
                    {
                        Name = "John Smith",
                        EmailAddress = "johnsmith@nowhere.com"
                    })
                });
     context.SaveChanges();
        }
    }

所以,我的问题是,是否可以在局部视图中只显示一条评论而不是两条评论?

0 个答案:

没有答案