Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I am assuming from your code that this is an Asp.Net MVC project, using C# as the language.</p> <p>There are a number of issues. The first is that the equipmentID variable is not being passed to the view. There are a number of ways to do this. The simplest will be to do it in your controller using the ViewData dictionary, as follows:</p> <pre><code>public ActionResult CreateReservation(int equipmentid) { // TODO: Query the piece of equipment being reserved. ViewData["equipmentID"] = equipmentid; return View(); } </code></pre> <p>Once this is done, you could modify your view code to use the value as follows:</p> <pre><code>@using (Html.BeginForm("CreateReservation", "ReservationList", FormMethod.Get, new { equipmentID = ViewData["equipmentID"] })) { &lt;button class="btn btn-blue"&gt;Click&lt;/button&gt; } </code></pre> <p>This is fine for one or two values, but when the view gets more complex, you rather want to create a ViewModel class, and pass that class to your view.</p> <p>Secondly - your use of the @ in front of equipmentID inside your BeginForm call indicates that you probably need to study up a bit on Razor syntax. A good place to start is <a href="http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx" rel="nofollow noreferrer">Phil Haack's Razor Syntax Quick Reference</a>. Razor syntax is what tells the parser which parts of your view are static HTML that it should output as is, and which parts are C# code that it should execute before sending to the browser. If you are using Visual Studio as your IDE, it is pretty good at highlighting which parts of your code are being interpreted as C# code, and which parts as ordinary HTML.</p> <p>In my screenshot below you can see that VS2012 highlights C# code with a light grey background colour. The @ symbol is the start of C# code - so you don't need to use it again unless you break out of the C# block and go back to HTML.</p> <p><img src="https://i.stack.imgur.com/g6tXL.png" alt="Example of how Razor C# code is highlighted in Visual Studio 2012"></p> <p>And thirdly, you probably wouldn't want to pass the equipment ID as an HTML attribute inside your form tag. You should rather create a hidden form field inside your form body and set the name to equipmentID, and the value to the variable's value. The way you are currently using it will add an HTML attribute to the form tag, and your generated code will look something like this:</p> <pre><code>&lt;form action="/ReservationList/CreateReservation" method="get" equipmentID="1"&gt; </code></pre> <p>And this extra attribute will not be retrievable in your code. Rather do something like this in your view:</p> <pre><code>@using (Html.BeginForm("CreateReservation", "ReservationList", FormMethod.Get, new { name = "myFormName", id = "myFormID" })) { @Html.Hidden("equipmentID", ViewData["equipmentID"]) &lt;button class="btn btn-blue"&gt;Click&lt;/button&gt; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload