Reputation: 2508
Continuing study Razor and web development at all (I'm C# desktop dev.). Trying to find easiest way to call action on controller with specified params by pressing submit input, but WITHOUT AJAX (yea, yea, with page reloading). Any ways to do that?
Upvotes: 1
Views: 81
Reputation: 539
You need to decorate your action with HttpPost:
[HttpPost]
Public ActionResult SendData(string name string address)
{ //use your parameters
//redirect to another action so refreshing the page doesn't cause a repost Return RedirectToAction("Index"); }
**note: this code was written on my phone and isn't tested, there maybe syntax issues.
Upvotes: 0
Reputation: 1636
I think this should be enough:
Controller:
public class UserController : Controller
{
public ViewResult Index()
{
return View();
}
public string SendData(string name, string address)
{
return string.Empty;
}
}
Razor:
@using (Html.BeginForm("SendData", "User"))
{
@Html.TextBox("Name")
@Html.TextBox("Address")
<button type="submit">Send</button>
}
You could also add a class that matches the posted items:
public class User
{
public string Name { get; set; }
public string Address { get; set; }
}
Then change the action to:
public string SendData(User user)
{
return string.Empty;
}
The default binder will bind to the relevant properties in the user.
Upvotes: 4