简体   繁体   English

Webmatrix中Response.Redirect的替代方法

[英]Alternative to Response.Redirect in Webmatrix

I am writing a check list for mechanics so everything uses radio buttons and pull down menus (blah!). 我正在为机械师编写一份检查清单,以便所有内容都使用单选按钮并下拉菜单(等等!)。 There could be over 50 items so passing values in the url would be messy. 可能有50多个项目,因此在url中传递值会很混乱。 If I send the form with POST all variable values get passed, but I need to use Response.Redirect to get the form to write to the database and that blocks the variables from being passed to the next page. 如果我通过POST发送表单,则所有变量值都会传递,但是我需要使用Response.Redirect来将表单写入数据库,这会阻止变量传递到下一页。 I have tried META refresh and Response.StatusCode = 307; 我已经尝试过META刷新和Response.StatusCode = 307; but nothing works so far. 但到目前为止没有任何效果。 Here is page one.... 这是第一页。

@{

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

        <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>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &rlm; &nbsp Head Lights:</label>
     <input type="RADIO"  name="HEADLIGHTS" value="GOOD"/>GOOD &nbsp &nbsp
     <input type="RADIO"  name="HEADLIGHTS" value="BAD"/>BAD

   </br>
     <label>&nbsp Tail/Stop/Signal/Hazard:</label>
     <input type="RADIO" name="TailStopSignalHazard" value="GOOD"/>GOOD &nbsp &nbsp
     <input type="RADIO" name="TailStopSignalHazard" value="BAD"/>BAD

 </p>

  <p><input type="submit" value="POST"/></p>
 </fieldset>
  </form>

 </body>

 </html>

And then I check the database and the value of "UNITNO" on page two with this... 然后我以此检查数据库和第二页上“ 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>
    &nbsp &nbsp &nbsp &nbsp the unit number was="@Request["UNITNO"]"
    </body>

      </html>

I am new to webmatrix and novice at script so please excuse my ignorance. 我是webmatrix和脚本的新手,因此请原谅我的无知。 My hope is that somebody has a simple solution to my problem, also I tried global variables but either I didn't format it properly or it doesn't work either. 我希望有人可以对我的问题有一个简单的解决方案,我也尝试了全局变量,但是我没有正确格式化它或它也不起作用。 Thanks for reading! 谢谢阅读!

You could pass some data to the second script on the redirect: 您可以将一些数据传递给重定向中的第二个脚本:

Response.Redirect("list.cshtml?UNITNO=" + Request.Form["UNITNO"]);

But if you need to send a lot of data, that could get messy. 但是,如果您需要发送大量数据,则可能会变得混乱。 What might work better is to send the database ID of the newly created record. 更好的方法是发送新创建记录的数据库ID。 You can then open it again on your second script and read and display whatever data you like: 然后,您可以在第二个脚本上再次将其打开,并读取并显示所需的任何数据:

Response.Redirect("list.cshtml?id=" + db.GetLastInsertId());

My own general rule is that if I'm doing a redirect and need to maintain data I should probably think about refactoring the code to not require it if at all possible - but the solution above should work. 我自己的一般规则是,如果我要进行重定向并需要维护数据,我可能应该考虑将代码重构为尽可能不要求它-但上述解决方案应该可行。

Try 尝试

Response.Redirect("list.cshtml/" + Request.Form["UNITNO"]);

I found that using 我发现使用

Response.Redirect("list.cshtml?UNITNO=" + Request.Form["UNITNO"]);

does work but if you use var UNITNO at the start of your form then the URL will pass list.cshtml=UNITO xxx whatever UNITNO is which does not always pass depending on how you get the URL var in your form. 确实可以,但是如果您在表单的开头使用var UNITNO,则URL将通过list.cshtml = UNITO xxx,无论UNITNO是什么,但并不总是通过,具体取决于您如何从表单中获取URL var。

Just replacing with ?UNITNO with a simple / then the request works well for me. 只需用一个简单的/ UNITNO替换/即可,该请求对我来说效果很好。

But the answer given already will also work so I am knocking it but I found I had problems and adapted the original answer with this method which worked for me. 但是给出的答案也可以使用,所以我敲了一下,但是我发现自己遇到了问题,并用适合我的这种方法修改了原始答案。

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM