I am struggling to implement a button on the client side of an html
page that would allow the user toggling between two different layouts invoked through corresponding view components. Here is what I started:
@{ bool display = Request.Form["display"] == "true";}<form method="post"><input type="hidden" name="display" value="@display.ToString().ToLower()" /> Display: <button type="submit"> @(display ? "Layout 2" : "Layout 1")</button></form>@if (display){<div><!-- First view component --><h2>Layout 1</h2></div>}else{<div><!-- Second view component --><h2>Layout 2</h2></div>}
I have an error message upon loading the page: "This request does not have a content-type header. Forms are available from requests with bodies like POSTs and a form content-type of either application/x-www-form-urlencoded or multi-part/form-data".
I have tried to amend the <form>
with the attribute enctype="application/x-www-form-urlencoded"
or enctype="multi-part/form-data"
with no success. I have also tried to declare the boolean variable "display" as
@{ bool display = false; if (Request.Method == "POST") { display = Request.Form["display"] == "true"; }}
bu then, the variable never gets amended upon clicking on the button, thereby showing that the statement Request.Method == "POST"
is false in general, since it turns out that, actually, Request.Method == "GET"
, in spite of the <form>
attribute method="post"
.