C# 使用部分视图更新asp.net ajax
试图在局部视图中仅显示文本框中的一条注释 要获取一些数据,您需要会话控制器:C# 使用部分视图更新asp.net ajax,c#,asp.net,ajax,asp.net-mvc-4,C#,Asp.net,Ajax,Asp.net Mvc 4,试图在局部视图中仅显示文本框中的一条注释 要获取一些数据,您需要会话控制器: private ConferenceContext context = new ConferenceContext(); // // GET: /Session/ public ActionResult Index() { ConferenceContext context = new ConferenceContext();
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>
@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>
_GetForSession从Comment文件夹中的_CommentForm获取数据:
@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>
@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>
现在,主要上下文将来自模型中的ConferenceContext:
public class ConferenceContext : DbContext
{
public DbSet<Session> Sessions { get; set; }
public DbSet<Speaker> Speakers { get; set; }
public DbSet<Comment> Comments { get; set; }
}
以及ConferenceContextInitializer中的上下文本身:
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();
}
}
因此,我的问题是,是否可以在局部视图中仅显示一条注释而不是两条注释?您始终希望显示一条注释,还是将其作为一个选项?此时,如果要显示注释,则会显示一条来自Models.comment的注释,另一条来自commentspublic类注释列表中的System.Data{public Int32 CommentID{get;set;}公共字符串内容{get;set;}公共Int32会话id{get;set;}公共虚拟会话会话{get;set;}