简体   繁体   中英

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. 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. I have tried META refresh and 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...

   @{
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. 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. 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.

Just replacing with ?UNITNO with a simple / then the request works well for me.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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