如何使用的Request.Form访问下拉文本(而不是值)()? [英] How to access dropdown text (not value) using Request.Form()?

查看:130
本文介绍了如何使用的Request.Form访问下拉文本(而不是值)()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的MVC 4.0。

我需要使用的Request.Form访问我的下拉框的文本(而不是值)(ddlId)在我的控制器code。

和显示注册确认页面上选定的信息。

即。让我们考虑,我下面有国家的Dropbox。

 <选择数据-VAL =真正的数据-VAL-所需=必需ID =CountryIdNAME =CountryId的风格=WIDTH:210px> <期权价值=>  - 选择 - < /选项><期权价值=1>美国< /选项><期权价值=2> UK< /选项>&LT ; /选择>

现在,在控制器当我使用,

  objWizard.CountryId =的Request.Form [CountryId];

我得到了国家的Dropbox的价值,由用户选择的不是文字。

我如何可以选择使用的Request.Form(...)???? Dropbox的文本

或者任何其他........

我的jQuery的code是如下图所示。

  $。员额('@ Url.Action(ConfirmDetails,精灵)',$(形式)。序列化(),功能(R)
                    {
                        //注入在确认步骤响应
                        //$(\".wizard-step:visible)
                        $(#confirmdiv)的HTML(R);
                    });


解决方案

我假设你要发送一个形式,而且喜欢看

 <形式为GT;
<选择ID =CountryId>
  <期权价值= 1>美国和LT; /选项>
  <期权价值= 2 - ; UK< /选项>
< /选择>
<输入类型=提交值=提交ID =btnSubmit按钮/>
< /表及GT;

在表单提交取消默认行为

  $(#btnSubmit按钮)。点击(函数(五){
亦即preventDefault();
//现在做一个隐藏字段在这里,并把所选选项的文本在
VAR selectedOption = $(#CountryId选项:选择)文本();
$(<输入/>中,{类型:隐藏,名称:国家或地区名称'})。VAL(selectedOption).appendTo(形式);
//现在发布形式
.post的$('@ Url.Action(ConfirmDetails,精灵)',$(形式)。序列化(),功能(R)
  {
   //注入在确认步骤响应
   //$(\".wizard-step:visible)
   $(#confirmdiv)的HTML(R);
  });
});

在控制器

  [HttpPost]
公众的ActionResult ConfirmDetails()
  {   VAR国家名称=的Request.Form [国家或地区名称];
  }

I am working on MVC 4.0.

I need to access my dropdown box Text (not value) using Request.Form("ddlId") in my controller code.

and display the selected info on confirmation page of registration.

i.e. lets consider I am having Country dropbox as below.

 <select data-val="true" data-val-required="Required" id="CountryId" name="CountryId" style="width:210px"><option value="">--Select--</option><option value="1">USA</option><option value="2">UK</option></select>

now, in controller when i use,

            objWizard.CountryId = Request.Form["CountryId"];

I got the value of COuntry dropbox, not the text selected by user.

How can i select text of dropbox using Request.Form(...)????

Or any alternative........

My jquery code is as below.

   $.post( '@Url.Action("ConfirmDetails", "Wizard")', $("form").serialize(), function (r)
                    {
                        // inject response in confirmation step
                        //$(".wizard-step:visible")
                        $("#confirmdiv").html(r);
                    });

解决方案

i'll assume that you are posting a form and that look likes

<form>
<select id="CountryId">
  <option value=1>US</option>
  <option value=2>UK</option>
</select>
<input type="submit" value="submit" id="btnSubmit"/>
</form>

on form submit cancel the default behavior

$("#btnSubmit").click(function(e){
e.preventDefault();
//now make a hidden field here and put the text of selected option in that 
var selectedOption = $("#CountryId option:selected").text();
$("<input/>",{type:'hidden',name:'CountryName'}).val(selectedOption).appendTo("form");
// now post the form 
$.post( '@Url.Action("ConfirmDetails", "Wizard")', $("form").serialize(),function (r)
  {
   // inject response in confirmation step
   //$(".wizard-step:visible")
   $("#confirmdiv").html(r);
  });
});

in the controller

[HttpPost]
public ActionResult ConfirmDetails()
  {

   var countryName = Request.Form["CountryName"];
  }

这篇关于如何使用的Request.Form访问下拉文本(而不是值)()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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