我正在MVC4.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 dropbox的值,而不是用户选择的文本。
如何使用Request.Form(...)选择下拉框的文本?
或任何其他选择.
我的jquery代码如下所示。
$.post( '@Url.Action("ConfirmDetails", "Wizard")', $("form").serialize(), function (r)
{
// inject response in confirmation step
//$(".wizard-step:visible")
$("#confirmdiv").html(r);
});发布于 2012-01-31 18:52:00
我将假设您正在发布一个表单,该表单如下所示
<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"];
}发布于 2012-01-31 18:34:42
您可以使用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 )
{
//...........
}发布于 2012-01-31 19:09:00
您可以在服务器上发出value标记作为国家/地区名称,从而避免对javascript的任何需要。
<select id="CountryId">
<option value="US">US</option>
<option value="UK">UK</option>
</select>https://stackoverflow.com/questions/9077910
复制相似问题