Reputation: 11
I'm building a MVC project and I have some problems to send the data from my view to the controller or apicontroller. I need to call a POST method in the apicontroller with the parameters or the object;
string fname = "Mark";
string lname = "Twain";
string address = "Some street";
or
An instance of the class Person.
Upvotes: 1
Views: 810
Reputation: 1675
You could use a post or many other ways to get the data, but if it's a form and you are using asp.net, Razor handles that for you. Html.BeginForm
assumes you have a model that will be updated by the user via texboxes or other controls. Then some button click will do the call. Notice <button type="submit">
tells the form that when the button is pressed the service is to be called and the model object is posted.
This answer is the bare bones of using Html.BeginForm
. You will need to dig a little deeper to get a good understanding.
@model Student
@using (Html.BeginForm("InsertStudent", "StudentInfocontroller"))
{
// style it appropriately
@Html.TextBoxFor(m => m.fName)
@Html.TextBoxFor(m => m.lName)
<button type="submit" class="btn">Submit</button>
}
Controller call.
[Route("InsertStudent")]
public async Task<IActionResult> InsertStudent(Student student)
{
// do something with the student object received. Like insert or update the database.
Repository.InsertOrUpdate(student); // assuming you have a repository.
return View("Confirmation", student); <--- let the user know?
}
Upvotes: 1