简体   繁体   中英

I'm trying to make a submit type button that, when clicked on, redirects the user to another Razor type page

I tried a solution I saw online but the function they gave me seems to, when the button is pushed, it redirects to me again.

<div class="btn-block">
                <button type="submit" id="btnSubmit" OnClick="btnSubmit_Click" href="/">Send</button>
            </div>

This is how that button is written in html code. The code in C# is this one:

protected IActionResult btnSubmit_Click(object sender, EventArgs e)
        {
            return RedirectToPage("L_page");
        }

My goal is to make the button, when clicked, redirect to a Razor page called L_page. Can you give me some tips or another funcion to use?

Note: I'm new at making websites and using ASP.NET so any other advice will be welcomed.

onclick like that is a client-side thing, it will bind to a javascript function in the browser. If that's what you want you could do this

<div class="btn-block">
                <button type="submit" id="btnSubmit" onclick="clickHandlerInBrowser()">Send</button>
</div>
<script>
function clickHandlerInBrowser(){
   window.location.href = "http://www.w3schools.com"; //or e.g. "L-page.html"
}
</script>
<input type="button" value="btnSubmit" onclick="window.location.href='<%= Url.Action("actionName", "controllerName") %>';" />

If you are using asp.net core mvc:

1.asp-controller,asp-action:

<a asp-controller="ControllerName" asp-action="ActionName">Send</a>

2.href:

<a href="/xxx/xxx">Send</a>

If you want to use button,you can also do like this:

<form asp-controller="ControllerName" asp-action="ActionName">
<button type="submit" id="btnSubmit">Send</button>
</form>

If you are using asp.net core razor page:

1.asp-page:

<a asp-page="../ControllerName/ActionName">Send</a>

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