从另一个局部视图更新局部视图-ASP.NET MVC2 [英] Update A Partial View From Another Partial View - ASP.NET MVC2

查看:101
本文介绍了从另一个局部视图更新局部视图-ASP.NET MVC2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拥有两个局部视图,一个用于SEARCH,一个用于SEARCHRESULTS.

I want to have two partial views, one for SEARCH and one for SEARCHRESULTS.

我要在SEARCH部分视图窗体上单击搜索"按钮时更新SEARCHRESULTS. SEARCHRESULTS需要从SEARCH部分视图中获取表单数据.

I want to update SEARCHRESULTS when the "Search" Button is clicked on the SEARCH partial view form. SEARCHRESULTS needs to have the form data fed to it from the SEARCH partial view.

我不太确定该怎么做.我可以从我的SEARCH局部视图的Controller动作中更新SEARCHRESULTS局部视图吗?

I'm not totally sure how to go about this. Can I update the SEARCHRESULTS partial view from my SEARCH partial view's Controller action?

推荐答案

一般性讨论
在MVC设计模式中,视图之间不会相互意识到.它们可以由组装多个局部视图的视图概念绑定在一起,但是即使那样,局部视图彼此也不了解.对于ASP.NET MVC,此概念是正确的. Mike Brind在他的文章

General Discussion
In the MVC design pattern views are unaware of each other. They may be bound together by the concept of a view assembling multiple partial views but even then the partials are ignorant of each other. This concept is true for ASP.NET MVC. Mike Brind does a good job describing partials and ViewData in his post ASP.NET MVC Partial Views and Strongly Typed Custom ViewModels.

特定于您的问题
为了回答您的问题,只要适当的信息传递到控制器,局部视图就可以链接到呈现不同视图的控制器动作.您如何进行此操作取决于您要尝试做的事情.

Specific to your Question
To answer your question a partial view can have a link to a controller action that renders a different view, so long as the appropriate information is passed to the controller. How you go about this will depend on what you are trying to do.

鉴于您的问题,我将假设SEARCH部分视图是带有搜索字段和按钮的简单表单.而SEARCHRESULTS是返回数据的列表.在这种情况下,您将创建一个名为Search的控制器动作,该动作将接收字符串值并仅返回SEARCHRESULTS部分或包含SEARCHRESULTS部分的视图. Scott Guthrie在他的博客文章

Given your question I am going to assume that the SEARCH partial view is a simple form with a search field and button. While SEARCHRESULTS is a listing of the returned data. In this instance you'd create a controller action called Search which takes in a string value and returns just the SEARCHRESULTS partial or a view containing the SEARCHRESULTS partial. Scott Guthrie provides a pretty good description of passing data to a view in his blog post Passing ViewData from Controllers to Views.

// returning partial
public ActionResult Search(string q)
{
    //do search .......
    //.................

    return PartialView("SEARCHREULTS", viewdata);
}

这篇关于从另一个局部视图更新局部视图-ASP.NET MVC2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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