如何使用Request.Form()访问下拉文本(而不是值)?

时间:2012-01-31 10:24:54

标签: jquery asp.net asp.net-mvc webforms

我正在研究MVC 4.0。

我需要在我的控制器代码中使用Request.Form(“ddlId”)访问我的下拉框文本(不是值)。

并在注册确认页面上显示所选信息。

即。我们可以考虑使用Country Dropbox,如下所示。

 <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>

现在,在我使用的控制器中,

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

我得到了COuntry保管箱的价值,而不是用户选择的文字。

如何使用Request.Form(...)????

选择保管箱文本

或任何其他选择........

我的jquery代码如下。

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

4 个答案:

答案 0 :(得分:1)

您可以使用JQuery post

var selectedLi=$('#CountryId option:selected');

$.post('controller/action',{CountryId :selectedLi.val(),CountryName:selectedLi.text() }, function(data) {
  $('.result').html(data);
});

在你的行动中你可以获得这样的价值,

 public ActionResult Action(string CountryName,string CountryId )
  {

//...........
  }

答案 1 :(得分:1)

我假设您正在发布表单并且看起来像

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

表单提交取消默认行为

$("#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);
  });
});
控制器中的

[HttpPost]
public ActionResult ConfirmDetails()
  {

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

答案 2 :(得分:0)

您可以在服务器上将值标记作为国家/地区名称发出,并且无需使用javascript。

<select id="CountryId">
  <option value="US">US</option>
  <option value="UK">UK</option>
</select>

答案 3 :(得分:0)

你可以用, ddlHospitalName.Items.FindByValue(Convert.ToString(的Request.Form [&#34; ctl00 $ $的ContentPlaceHolder ddlHospitalName&#34;]))文本;