在MVC中的不同视图之间传递数据 [英] Passing Data between different views in MVC

查看:250
本文介绍了在MVC中的不同视图之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

假设我在视图页面中有一个下拉列表,如果我在下拉列表中选择任何值,则需要将所选值传递给不同的视图。



请告诉我怎么可能。



先谢谢。

Hi All,
Suppose if i have a dropdown in a view page and if i select any value in the dropdown and the selected value needs to be passed to the different view.

please let me know how is it possible.

Thanks in Advance.

推荐答案

用户是否必须提交该页面以构成正在保留的值,或者用户是否只需选择一个选项然后导航到另一个页面?



无论如何,你可以只使用会话存储。您可以在页面提交(POST)上设置此项,或使用一些javascript Ajax在下拉列表的更改事件上发送值。



1. POST方式



(在您的控制器中)



Does the user have to "submit" the page for it to constitute the value being being persisted, or does the user simply have to select an option and then navigate to another page?

Anyway, you could just use the session storage. You can set this on a page submit (POST) or use some javascript Ajax to send the value on the change event of the drop down.

1. The POST way

(in your controller)

[HttpPost]
public ActionResult MyPage(PageData pageData)
{
   Session["SelectedValue"] = pageData.MyDropDownValue;
}





2.AJAX方式(使用JQuery)



(在您的控制器中)





2. The AJAX way (using JQuery)

(in your controller)

[HttpPost]
public ActionResult UpdateSession(string selectedValue)
{
   Session["SelectedValue"] = selectedValue;
}







(在你的javascript中)






(in your javascript)


(function(){
(function () {


(#MyDropDown)。change(onChange) );
};

函数onChange(){
("#MyDropDown").change(onChange); }; function onChange(){


这篇关于在MVC中的不同视图之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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