How to prevent Ajax.BeginForm from loading a new page?

I have an Ajax form with one input and a button. When submitting, the form should publish only the entered value in the action method. My form is placed correctly in the log method of the user controller, but when this is done, the page is redirected to / User / Log.

How can i avoid this?

<% using (Ajax.BeginForm("Log", "User", null, new AjaxOptions {HttpMethod = "POST" })) {%> Please enter the username: <%= Html.TextBox("Username", "")%> <input type="submit" /> <% } %> 

Here is the mode of action:

 public class UserController : Controller { [AcceptVerbs(HttpVerbs.Post)] public void ForgotPassword(FormCollection collection) { string username = collection["Username"]; //TODO: some work } } 

Thanks, -Keith

+4
source share
2 answers

to add a new object {target = "_self"} as an htmlAttributes object for me, as in

 <% using (Ajax.BeginForm("Log", "User", null, new AjaxOptions {HttpMethod = "POST" }, new { target = "_self" })) {%> 
+2
source

You can change your controller code to:

 public class UserController : Controller { [AcceptVerbs(HttpVerbs.Post)] public void ForgotPassword(string Username) { //TODO: some work } } 

I copied your code and everything works fine for me. Shouldn't it be Ajax.BeginForm("ForgotPassword", "User" instead of Ajax.BeginForm("Log", "User" in the code?

0
source

Source: https://habr.com/ru/post/1302417/


All Articles