我正在为机械师写一个检查清单,所以一切都使用单选按钮和下拉菜单(废话!)。可能有超过 50 个项目,因此在 url 中传递值会很混乱。如果我使用 POST 发送表单,所有变量值都会被传递,但我需要使用 Response.Redirect 来让表单写入数据库并阻止变量传递到下一页。我已经尝试过 META 刷新和 Response.StatusCode = 307; 但到目前为止没有任何效果。这里是第一页......
@{
var UNITNO = "";
var DATE = DateTime.Now;
var MECHANIC = "";
var HEADLIGHTS = "";
var TailStopSignalHazard = "";
if(IsPost)
{
UNITNO = Request.Form["UNITNO"];
MECHANIC = Request.Form["MECHANIC"];
HEADLIGHTS = Request.Form["HEADLIGHTS"];
TailStopSignalHazard = Request.Form["TailStopSignalHazard"];
var db = Database.Open("MAINT");
var insertCommand = "INSERT INTO CHECKLIST (UNITNO, DATE, MECHANIC, HEADLIGHTS, TailStopSignalHazard) Values(@0, @1, @2, @3 ,@4)";
db.Execute(insertCommand, UNITNO, DATE, MECHANIC, HEADLIGHTS, TailStopSignalHazard);
Response.StatusCode = 307;
Response.Redirect("list.cshtml");
//<META http-equiv="REFRESH" content="0; url=list.cshtml">
}
<script type="text/javascript">
function validateForm() {
var aa = document.getElementById("UNITNO");
var ac = document.getElementById("MECHANIC");
var xx = document.getElementsByName("HEADLIGHTS");
var xz = document.getElementsByName("TailStopSignalHazard");
if (aa.options[aa.selectedIndex].index == 0) { alert("Must enter Unit Number"); return false; }
if (ac.options[ac.selectedIndex].index == 0) { alert("Must enter Mechanic"); return false; }
if (xx[0].checked == false && xx[1].checked == false) { alert("Must enter Headlights"); return false; }
if (xz[0].checked == false && xz[1].checked == false) { alert("Must enter Tail/Stop/Signal/Hazard"); return false; }
}
</script>
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Western Check List</title>
</head>
<body>
<h1>Western Disposal</h1>
<!-- <form method="post" onsubmit="return validateForm()" action="list.cshtml"> //-->
<form method="post" onsubmit="return validateForm()">
<fieldset>
<legend>Check List</legend>
           
<script>
date=Date()
document.write(date)
</script>
<p>
<label> Unit Number: </label>
<select name="UNITNO">
<option value=""></option>
<option value="108">108</option>
<option value="110">110</option>
<option value="111">111</option>
</select>
</p>
<p>
<label>Mechanic:</label>
<select name="MECHANIC" >
<option value=""></option>
<option value="BOB">BOB</option>
<option value="DANIEL">DANIEL</option>
<option value="DOUG">DOUG</option>
</select>
</p>
<p>
<b> LIGHTING: </b>
</br>
<label>                  ‏   Head Lights:</label>
<input type="RADIO" name="HEADLIGHTS" value="GOOD"/>GOOD    
<input type="RADIO" name="HEADLIGHTS" value="BAD"/>BAD
</br>
<label>  Tail/Stop/Signal/Hazard:</label>
<input type="RADIO" name="TailStopSignalHazard" value="GOOD"/>GOOD    
<input type="RADIO" name="TailStopSignalHazard" value="BAD"/>BAD
</p>
<p><input type="submit" value="POST"/></p>
</fieldset>
</form>
</body>
</html>
然后我用这个检查第二页上的数据库和“UNITNO”的值......
@{
var db=Database.Open("MAINT");
var grid=new WebGrid(db.Query("SELECT * FROM CHECKLIST ORDER BY ID"));
}
<!DOCTYPE html>
<html>
<head>
<title>Western Check List Data Base</title>
<style type="text/css">
table {border-collapse: collapse;}
td, th {border: solid 1px; }
</style>
</head>
<body>
<h1>Western Check List Data Base</h1>
@grid.GetHtml()
<a href="Default.cshtml">RETURN</a>
        the unit number was="@Request["UNITNO"]"
</body>
</html>
我是 webmatrix 的新手和脚本新手,所以请原谅我的无知。我希望有人对我的问题有一个简单的解决方案,我也尝试了全局变量,但要么我没有正确格式化它,要么它也不起作用。谢谢阅读!