MVC与控制器的局部视图,阿贾克斯 - 我怎么GE局部控制器获取数据? [英] MVC Partial view with controller, ajax - how do I ge the partial controller to get data?

查看:88
本文介绍了MVC与控制器的局部视图,阿贾克斯 - 我怎么GE局部控制器获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习MVC和我这个难住了。我想一些因素共同code,获取数据并显示在一个进入的WebGrid的局部视图,我可以在多个页面中使用。

I'm learning MVC and am stumped by this. I'm trying to factor some common code that gets data and displays it in a WebGrid into a partial view that i can use on multiple pages.

我家的控制器Index方法只是做一回视图()。主页视图看起来是这样的:

My home controller Index method just does a return View(). The Home view looks like this:

@using (Ajax.BeginForm("SearchAction", "Search",
    new AjaxOptions { UpdateTargetId = "data-grid", HttpMethod = "Post" }))
{
    @Html.TextBoxFor(model => model.name)
    <input type="submit" value="Search" />
}

@{
<div id="data-grid">
    @Html.Partial("SearchResults", Model)
</div>
}

我想使用Ajax,以避免点击一个寻呼机的WebGrid链接,它呈现为正常的链接时,失去我的搜索表单数据。

I'm trying to use Ajax to avoid losing my search form data when clicking a WebGrid pager link, which are rendered as normal links.

我SearchController看起来是这样的:

My SearchController looks like this:

public ActionResult SearchAction(string name)
{
    return RedirectToAction("SearchResults", new { name = name });
}

public ActionResult SearchResults(string name)
{
    //does database query and sticks results in the viewbag
    //filter on optional name parameter

    VieweBag.Members = MyQueryResults;
    return PartialView();
}

共享视图

我的SearchResult,数据通过ViewBag.Members传递:

My SearchResults shared view, data is passed in via ViewBag.Members:

@{
    var grid = new WebGrid(null, rowsPerPage: ViewBag.Pagesize);
    grid.Bind(ViewBag.Members);
    @grid.GetHtml(// etc. etc.)
}

我得到的结果是,ViewBag.Pagesize和ViewBag.Members绑定,因为有在viewbag没有数据失败。很显然,我的部分控制器不被称为做初步查询,并把东西在ViewBag第一次加载主页时。我如何做到这一点?

The results I'm getting is that the ViewBag.Pagesize and ViewBag.Members binding fails since there is no data in the viewbag. Obviously, my partial controller is not being called to do the initial query and put stuff in the ViewBag when the home page is first loaded. How do I make that happen?

另外奇怪的是,如果我只是数据库查询code复制到我家控制器(它原本是),迫使原来的查询,那么如果我把一些文字在搜索字段中,做一个搜索,局部视图呈现本身就是一个新的页面上。为什么会发生这种情况,我认为它只会呈现为我的主页的一部分。

The other weird thing is that if I just copy the database query code into my home controller (where it originally was) to force the original query, then if I put some text into the search field and do a search, the partial view renders by itself on a new page. Why is that happening, I thought it would only render as part of my home page.

我一起从不同的答案/地方鹅卵石这个局部视图,并毫无悬念地得到了可怕的错误的东西:\\

I've cobbled this partial view together from various answers/places and have no doubt gotten something horribly wrong :\

推荐答案

局部页面不会通过控制器,而只是直接渲染视图。如果你想查看数据传递到局部视图,有一个重载函数,它接受一个可视数据字典。对不起,我不能更详细,但我对我的手机(等待我的儿子在另一个房间里睡着了):)

The partial page won't pass through a controller, but simply render the view directly. If you want to pass view data to the partial view, there is an overloaded function that takes a viewdata dictionary. I'm sorry I can't be more detailed, but I'm on my mobile (waiting for my son to fall asleep in the other room) :)

更新:

如果你想触发 GET 行动的局部视图,您可以使用 Html.Action 。下面是一些有用的链接:

If you want to trigger a GET action for your partial view, you can use Html.Action. Here are some useful links:

  • MSDN RenderAction
  • Difference between RenderPartial and RenderAction

此外,它很可能让你感觉你的表单标签进入你的部分观点,但这些都是当你清理code的详细信息。

Further, it would probably make sense for you to move your form tags into your partial view, but those are details for when you clean up the code.

这篇关于MVC与控制器的局部视图,阿贾克斯 - 我怎么GE局部控制器获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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