MVC下拉列表保存更改后的选定值 [英] MVC drop down list save selected value onchange

查看:75
本文介绍了MVC下拉列表保存更改后的选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好;
如何在onchange事件发生后将所选值存储在会话变量内的下拉列表(ASP.NET/MVC)中?
我在ascx中的代码

Hello;
How can I store a selected value in a drop down list (ASP.NET/MVC) within a session variable after the onchange event?
My code in ascx

<table width="150" border="0" cellspacing="0" cellpadding="0" style="margin-removed 1px; margin-removed 1px; margin-removed 1px;">
    <tr>       
        <td class="formrechinp">
            <%= Html.DropDownList("localSite",
                                          SiteHelper.GetSitesList((CultureInfo) Session["Culture"],
                                                                  menuState.Site_Name),
                                          new
                                              {
                                                  onChange = "javascript:ChangeLocalSite(this.value);"
                                              }) %>         
        </td>
    </tr>
</table>



我在CS中的代码:



My code in cs:

public void ChangeLocalSite(String value)
       {
           Session["localSite"] = value;
       }

推荐答案

ChangeLocalSite是javascript方法,仅在那个" html页面上可用.但是Session ["localSite"]在(Web)服务器中.

要设置会话变量,您可能必须从javascript执行服务器方法.这是通过ajax调用做到这一点的唯一可能方法.不要害怕ajax.学习和使用ajax方法非常容易.

这是使用jquery的示例:


ChangeLocalSite is javascript method and it is available only on "that" html page. But the Session["localSite"] is in the (web)server.

To set Session variable you may have to execute a server method from javascript. This only possible way of doing this through ajax call. Donot be scared of ajax. It is very easy to learn and use an ajax method.

here is an example using jquery:


   public void ChangeLocalSite(String value)
        {


.ajax({ url:" , dataType:' json', 类型:' POST', 数据:, 成功:功能(数据){ 如果(数据!= ) alert(data); // 应该显示找到它! } } }); } // 此方法位于YourController.cs [HttpPost] 字符串 MyMethod(字符串 ) { 会话[" ] = ; 返回 " ; }
.ajax({ url: "MyMethod", dataType: 'json', type: 'POST', data:value, success: function (data) { if (data != null) alert(data);//should display found it! } } }); } //this is method is in YourController.cs [HttpPost] string MyMethod(string value) { Session["localSite"] = value; return "found it!"; }


这篇关于MVC下拉列表保存更改后的选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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