如何在MVC3中使用ViewBag更改或刷新数据 [英] How To Change or refresh data using ViewBag in MVC3

查看:575
本文介绍了如何在MVC3中使用ViewBag更改或刷新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用ViewBag刷新视图中的数据.有可能还是我可以使用其他一些技术?...

I want to refresh the data in the view using ViewBag. Is it possible or may i use some other technique....

这是我的示例代码

在我的视图"页面中( FamilyDetails )

In My View Page ( FamilyDetails)

@ViewBag.Details[0].Name

在我的控制器中

public ActionResult FamilyDetails(string StrText)
    {
        if (StrText != null)
        {
            ViewBag.MarriageDetails = ListMarriages;
        }
        else
            ViewBag.MarriageDetails = ListMarriages1;

请用代码解释.

预先感谢

推荐答案

您不会重新加载视图.浏览器发送一个请求,该请求由控制器处理,然后控制器创建视图模型(在本例中为视图包),然后选择将视图模型传递到的视图.

You don't reload a view. The browser sends a request which is handled by a controller which then creates the view model (in this case the viewbag) then selects a view to which the view model is passed on.

您需要在控制器中用所需的数据填充视图包.这就是asp.net mvc应用程序的工作方式.

You need to populate the view bag with the data you want in the controller. This is how an asp.net mvc application works.

如果要通过ajax更新视图,则只需使用jquery调用另一个操作,该操作将以json或html的形式返回所需的数据,这些数据将由javascript插入.但是在这种情况下,控制器只会返回一个json结果.

If you want to update the view via ajax, then you just use jquery to invoke another action which will return the data you want as json or html, which it'll be inserted by the javascript. But in this case, the controller just returns a json result.

我认为您确实需要学习asp.net mvc.官方站点具有基本教程 http://www.asp.net/mvc/tutorials .

I think you really need to learn about asp.net mvc. The official site has basic tutorials http://www.asp.net/mvc/tutorials .

这篇关于如何在MVC3中使用ViewBag更改或刷新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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