CusterN
CusterN

Reputation: 93

What is the right way to call a controller action from a razor page?

I'm wondering about the "right" way to do things rather than a hacky way.

I have an application that has both razor pages (MVVM?) and controllers from MVC. My controllers do specific things like work with phone calls in one and text messages in another.

I now have a razor page that has a button and I want the OnPost of this button to execute some actions on one or both of those controllers. As I'm reading different ways to do this, I get the impression that my razor page shouldn't interact directly with my controllers, but should use some intermediate class.

I've tried RedirectToAction and also creating an instance of the controller in the razor page but neither one feels right when all I want to do is say "MessageController.SendMessage("5555555555","What's up?")

Upvotes: 0

Views: 269

Answers (1)

Hien Nguyen
Hien Nguyen

Reputation: 18973

You should use Form Post for your requirement as

@using (Html.BeginForm("SendMessage", "Message", FormMethod.Post))
{
    <input type="text" name="textmessage" />
    <input type="number" name="phonenumber" />

    <input type="submit" value="Send" />
}

Upvotes: 1

Related Questions