什么是最" MVC"标准的方式来刷新页面MVC的/更新的一部分? [英] What's the most "MVC" standard way to refresh / update part of an MVC page?

查看:129
本文介绍了什么是最" MVC"标准的方式来刷新页面MVC的/更新的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页,显示的数据的表。我的页面有一些元素,例如,当元素改变一个Ajax调用对数据库所做的和元素在数据库中更新的jQuery。

I have a web page that shows a table of data. My page has jQuery on some elements such that when the elements change an Ajax call is made to the database and the element is updated in the database.

我也有一个需要某个时候彻底刷新数据网格。这似乎对我有这样做的两种方式:

I also have a need to sometime completely refresh the data grid. It would seem to me there are two ways of doing this:

1)使用AJAX调用我的控制器行动,检查是否是使用Request.IsAjaxRequest一个Ajax请求,返回HTML.Partial到调用Ajax和有更新的HTML的JavaScript。我有这样的方法,90%已经工作。

1) Use Ajax to call my controller action, check if is an Ajax request using Request.IsAjaxRequest, return HTML.Partial to the javascript that called the Ajax and have that update the HTML. I have this method 90% working already.

<一个href=\"http://stackoverflow.com/questions/8782879/how-can-i-$c$c-a-refresh-of-a-html-renderpartial-using-ajax-in-mvc3/8782887#comment10958218_8782887\">$p$pvious计算器问题

2)使用以下内容:

<div id="#thingy">@Html.Action("Detail", new { ac, me })</div>  
$('#thingy').load("@Server.JavaScriptStringEncode(Url.Action("Detail", new { ac, me }))"); 

现在我仍然在试图让第一工作方法,而且可以看第二种方法和思维,它看起来轻松了许多。

Now I am still trying to get the first method to work but also looking at the second method and thinking it looks a lot easier.

我想写code的作品也code符合大多数做事的方式MVC。考虑到这一点。这可能是最好的方法是什么?我所关心的一件事是在@ Server.Java ... code。我以前从来没有看到这一点,我想知道这是怎么用MVC处理,完成了所有的正常的安全得到应用?

I want to write code that works but also code that conforms most to the MVC way of doing things. With this in mind. Which might be the best method? One thing that concerns me is the @Server.Java... code. I have never seen this before and I am wondering how this is handled with MVC, does all the normal security get applied?

推荐答案

梅丽莎,

我倒是有partialview几乎可以肯定棒(和 @RenderPartial()如果视图模式是一样的在partialview,否则你可能会选择在 @RenderAction()助手来调用整个图,其中基本数据类型从基本视图数据类型不同显著刷新数据。

I'd almost definitely stick with a partialview (and @RenderPartial()) IF the model in the view is the same as that in the partialview, otherwise you could opt for the @RenderAction() helper to invoke data refreshes across your view where the base data type varies significantly from the base view data type.

您就会知道,从你的应用程序,它的方法是最适合使用情况的问题,但绝对是的RenderPartial()将是方法上的重大,在需要更新阿贾克斯在稍后一点在片更新在HTML中的DIV。

You'll know from your app which approach is best suited to the use case in question, but definitely the RenderPartial() would be the approach to major on, where ajax updates are required at a later point in the piece to update the html in the div.

- 这里的我怎么会看到这个列明做法:

[edit] - here's how i would see this set out in practice:

// this is a div contained within the main Detail.cshtml view
<div id="#thingy">@Html.RenderPartial("_Detail", Model)</div>

这将在一号通自动填充从对一个叫_Detail.cshtml partialview模型中的股利。随后,你将调用你的Ajax目标通过它返回的partialview和模型按照上述的动作同样格(#thingy)。

this would automatically on 1st pass populate the div from the Model against a partialview called _Detail.cshtml. Subsequently, you would invoke your ajax to target this same div (#thingy) via an action which returned the partialview and model as per the above.

即:

public ActionResult Detail(string ac, string me) {     
   vm.AdminDetails = _link.Detail(ac + me).ToList();      
   if (Request.IsAjaxRequest())         
      return PartialView("_Detail", vm);     
   return View(vm); 
}

希望这是有道理的。

Hope this makes sense.

好运气

这篇关于什么是最&QUOT; MVC&QUOT;标准的方式来刷新页面MVC的/更新的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆