C# 如何在MVC中刷新页面
我发现我的页面实际上是重新加载/刷新的,但是需要额外的重新加载才能看到我刚才添加的内容。但是,我必须再次重新加载以查看数据,或添加另一个数据以查看以前的数据 我在下面添加了控制器代码: (CreateComment和Comment(display comments)在详细信息(书籍详细信息)视图中) CreateComment:C# 如何在MVC中刷新页面,c#,asp.net-mvc,C#,Asp.net Mvc,我发现我的页面实际上是重新加载/刷新的,但是需要额外的重新加载才能看到我刚才添加的内容。但是,我必须再次重新加载以查看数据,或添加另一个数据以查看以前的数据 我在下面添加了控制器代码: (CreateComment和Comment(display comments)在详细信息(书籍详细信息)视图中) CreateComment: public ActionResult CreateComment(Guid id) { return View(new CommentToBo
public ActionResult CreateComment(Guid id) {
return View(new CommentToBook { BookId = id });
}
[HttpPost]
public ActionResult CreateComment(CommentToBookVm model) {
if (ModelState.IsValid) {
var m = new CommentToBook { Comment = model.Comment, BookId = model.BookId };
m.UserId = new Guid(Session["UserID"].ToString());
m.CreatedDate = DateTime.Now;
db.CommentToBooks.Add(m);
db.SaveChanges();
}
return View(model);
}
查看createComment
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.HiddenFor(s => s.BookId)
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Comment, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Comment, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Comment, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
看法
视图:
在执行当前操作期间发生未处理的异常
网络请求。有关详细信息,请查看堆栈跟踪
错误及其在代码中的起源
异常详细信息:System.NullReferenceException:对象引用不存在
设置为对象的实例
源错误:
第9行:@Html.Action(“Comment”,new{id=Model.id})
或
不允许子操作执行重定向操作
如果我尝试为CreateComment
等重定向到操作
我希望能举一个代码的例子,因为我发现仅仅用文字很难理解新概念 您的代码正在返回
CreateComment
方法的视图。看起来您仅将此操作方法标记为ChildActions
。您不应该在这样的用例中使用ChildAction。应使用ChildActions向视图呈现某些内容。示例:应用程序中的菜单栏
即使从CreateComment操作方法中删除[ChildAction]
,当您将模型返回表单时,它也将呈现CreateComment视图生成的标记。这意味着您将丢失注释列表(在调用Details视图时加载)
理想情况下,对于所有数据插入用例,您应该遵循p-R-G模式
PRG代表POST-重定向-获取。这意味着,您提交了一个表单,在成功地将数据保存到db之后,您将向客户端返回重定向结果,客户端(浏览器)将为GET action方法发出一个全新的http请求,在该请求中,您将查询db表并返回结果
但是,由于表单是通过调用Html.Action
方法加载到主视图的,因此无法获得所需的结果(同一视图中的注释和验证消息列表)。让它工作的一件事是,通过启用不引人注目的客户端验证。在这种情况下,表单实际上不会提交到服务器。相反,将调用客户端验证,并在同一页面中向用户显示验证消息(无页面重新加载!)
您可以通过在视图(或布局)中添加对这两个脚本的引用来启用它
另一个不依赖客户端验证的选项是创建平面视图模型,该模型包含新注释表单的现有注释和属性列表。提交表单时,如果验证失败,请重新加载Comments属性并将视图模型返回表单
public class ListAndCreateVm
{
[Required]
public string NewComment { set;get;}
public Guid BookId { set;get;}
public List<CommentVm> Comments { set;get;}
}
public class CommentVm
{
public string Comment { set;get;}
public string Author { set;get;}
}
您的代码正在返回
CreateComment
方法的视图。看起来您仅将此操作方法标记为ChildActions
。您不应该在这样的用例中使用ChildAction。应使用ChildActions向视图呈现某些内容。示例:应用程序中的菜单栏
即使从CreateComment操作方法中删除[ChildAction]
,当您将模型返回表单时,它也将呈现CreateComment视图生成的标记。这意味着您将丢失注释列表(在调用Details视图时加载)
理想情况下,对于所有数据插入用例,您应该遵循p-R-G模式
PRG代表POST-重定向-获取。这意味着,您提交了一个表单,在成功地将数据保存到db之后,您将向客户端返回重定向结果,客户端(浏览器)将为GET action方法发出一个全新的http请求,在该请求中,您将查询db表并返回结果
但是,由于表单是通过调用Html.Action
方法加载到主视图的,因此无法获得所需的结果(同一视图中的注释和验证消息列表)。让它工作的一件事是,通过启用不引人注目的客户端验证。在这种情况下,表单实际上不会提交到服务器。相反,将调用客户端验证,并在同一页面中向用户显示验证消息(无页面重新加载!)
您可以通过在视图(或布局)中添加对这两个脚本的引用来启用它
另一个不依赖客户端验证的选项是创建平面视图模型,该模型包含新注释表单的现有注释和属性列表。提交表单时,如果验证失败,请重新加载Comments属性并将视图模型返回表单
public class ListAndCreateVm
{
[Required]
public string NewComment { set;get;}
public Guid BookId { set;get;}
public List<CommentVm> Comments { set;get;}
}
public class CommentVm
{
public string Comment { set;get;}
public string Author { set;get;}
}
谢谢@Shyju;我用的是这个图案,但我不知道它有名字!嗯,它无法解析参数<代码>注释和
详细信息
方法在同一控制器内。“将文本表示为一系列Unicode字符”无法解析哪个参数?我将“Comment”更改为“Details”,作为RedirectToAction
的操作方法名称。你在尝试那个版本吗?@Hackerman对许多模式都是正确的。我们在代码库中使用它,但我们没有意识到我们正在使用它!PRG对于任何形式的提交都是必须的,因为它有效地防止了重复提交。是的。也解决了!我没有在C文件中尝试代码。写在这里。所有这些小问题。希望这是最后一次。感谢您指出@skylakeTha
public ActionResult Comment(Guid? id) {
var comment = db.CommentToBooks.Where(c => c.BookId == id);
return View(comment.ToList());
}
<table class="table">
<tr>
<th>
User
</th>
<th>
@Html.DisplayNameFor(model => model.Comment)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.ActionLink(item.User.UserName, "VisitUser", new { id = item.UserId })
</td>
<td>
@Html.DisplayFor(modelItem => item.Comment)
</td>
</tr>
}
</table>
Description:
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
[HttpPost]
public ActionResult CreateComment(CommentToBookVm model)
{
if (ModelState.IsValid)
{
//your existing code to save data to the table
return RedirectToAction("Details","Book", new { id=model.BookId} );
}
// Hoping that the below code might not execute as client side validation
// must have prevented the form submission if there was a validation error.
return View(model);
}
public class ListAndCreateVm
{
[Required]
public string NewComment { set;get;}
public Guid BookId { set;get;}
public List<CommentVm> Comments { set;get;}
}
public class CommentVm
{
public string Comment { set;get;}
public string Author { set;get;}
}
public ActionResult Details(Guid id)
{
var vm = new ListAndCreateVm { BookId= id};
vm.Comments = GetComments(id);
return View(vm);
}
private List<CommentVm> GetComments(Guid bookId)
{
return db.CommentToBooks.Where(c => c.BookId == bookId)
.Select(x=> new CommentVm { Comment = x.Comment})
.ToList();
}
@model ListAndCreateVm
@foreach(var c in Model.Comments)
{
<p>@c.Comment</p>
}
<h4>Create new comment</h4>
@using(Html.BeginForm())
{
@Html.ValidationSummary(false, "", new {@class = "text-danger"})
@Html.TextBoxFor(s=>s.NewComment)
@Html.HiddenFor(f=>f.BookId)
<input type="submit" />
}
[HttpPost]
public ActionResult Details(ListAndCreateVm model)
{
if(ModelState.IsValid)
{
// to do : Save
return RedirectToAction("Details,"Book",new { id=model.BookId});
}
//lets reload comments because Http is stateless :)
model.Comments = GetComments(model.BookId);
return View(model);
}