我在页面之间传递变量时遇到了问题。这里我有一个下拉菜单,允许选择是或否两个选项。默认为选择。
<form method="post" id="Form" action="/admin/DefCon5">
<table align="center" style="width: 200px; height: 140px;">
<tr>
<td colspan="5"><img src="/images/logo.png"><br> </td>
</tr>
</table>
<div align="center">
<select id = "DefCon5" align="center" valign="top" name="DefCon5">
<option value="Choose">Choose..</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
<br />
         
<textarea align="Center" name="message" value="message" rows="4" cols="40">Please enter a brief message before engaging DefCon5</textarea>
<br />
<input type="submit">
</div>
这里我选择了这两个选项之一(出于测试目的我选择了是)
string yes = Request.Params["Yes"];
string no = Request.Params["No"];
var DefCon5 = Request["DefCon5"];
string message = Request.Params["message"];
string redirURL = "";
if (DefCon5 == "yes")
{
Response.Write(@message);
Response.Write(@Defcon);
redirURL = "/somepage";
}
else
{
redirURL = "/DefCon5";
}
我注意到 if 语句失败了,因为 DefCon5 的读数与 yes 不完全一样。我尝试使用上一页中的变量,最后尝试使用文字字符串“Yes”。我也尝试了几个不同的请求,没有任何变化。我相信我可能错误地传递了是/否,但我不清楚为什么会这样。
最佳答案
我认为您应该在 Controller 代码中进行这样的更改
public ActionResult DefCon5(string DefCon5,string message)
{
//string yes = Request.Params["Yes"];
//string no = Request.Params["No"];
//var DefCon5 = Request["DefCon5"];
//string message = Request.Params["message"];
string redirURL = "";
if (DefCon5.ToLower() == "yes")
{
Response.Write(@message);
//Response.Write(@Defcon);
redirURL = "/somepage";
}
else
{
redirURL = "/DefCon5";
}
// your code here
return View();
}
关于c# - .Net 下拉菜单 c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47782097/