c# - .Net 下拉菜单 c#

标签 c# asp.net razor

我在页面之间传递变量时遇到了问题。这里我有一个下拉菜单,允许选择是或否两个选项。默认为选择。

 <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>&nbsp;</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 /> 
        &nbsp &nbsp &nbsp &nbsp &nbsp
        <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/

相关文章:

c# - 一次只触发一个自定义验证器

c# - 用户下载提示后删除文件

javascript - 从父窗口javascript调用子窗口函数

javascript - 如何使用 jQuery 重置我的 Ajax.Begin 表单字段值

javascript - 如何启用另存为弹出窗口?

c# - Linq to NHibernate 返回与 HQL 不同的结果?

c# - 避免在锁内和锁外重复if语句

ASP.NET 核心 Web API 授权属性返回 404 错误并强制重定向

c# - 事件日志有错误。 "An unhandled exception has occurred"

c# - 如何在我的 MVC 应用程序的 web.config 中允许 unicode 字符?