Reputation: 41
I had my submit form working from my razor file to a controller, then from the controller to my remote database. but now i don't even think the controller class is being called.
Here is my view:
@model InputEvent
@using (Html.BeginForm("Save", "Portal/Controllers/MyEvent"))
{
<md-input-container class="md-block" flex-gt-xs="">
<label>Title</label>
@Html.TextBoxFor(m => m.title)
</md-input-container>
<md-input-container class="md-block">
<label>Address</label>
@Html.TextBoxFor(m => m.address)
</md-input-container>
<md-button class="md-raised">
<input type="submit" value="Save" />
</md-button>
}
with my model:
public class InputEvent
{
public string title;
public string address;
}
And my controller with the database connection:
namespace Portal.Controllers
{
public class MyEventController : Controller
{
[HttpPost]
public ActionResult Save(InputEvent y)
{
MySqlConnection conn = new MySqlConnection("mydbstring");
string myTitle = y.title;
string myAddress = y.address;
conn.Open();
MySql.Data.MySqlClient.MySqlCommand comm = conn.CreateCommand();
comm.CommandText = "INSERT INTO event(title, address) VALUES(@title, @address)";
//comm.Parameters.AddWithValue("@title", myTitle);
//comm.Parameters.AddWithValue("@address", myAddress);
comm.Parameters.AddWithValue("@title", "test_title");
comm.Parameters.AddWithValue("@address", "test_address");
comm.ExecuteNonQuery();
conn.Close();
return View();
}
}
}
Am i not calling my controller correctly? or is my sql command invalid?
EDIT: I just checked my database again over an hour later, and i have multiple rows with "test_title" and "test_address" in there. i guess my code works, but it is VERY delayed. This might not be the best place to ask, but does anyone have any idea why it could be so delayed inserting into the DB?
Upvotes: 2
Views: 4389
Reputation: 27039
I see that your code is working now. However, you should not be returning a view from a POST method the way you are. You should use PRG pattern. Read that link so you are aware of the issues your code can cause. I know that is for MVC 4 but the pattern is the same regardless of the version of MVC.
Upvotes: 0
Reputation: 1633
Ensure the server-side code looks like the code below. Note the [HttpPost]
attribute. While the default for client-side form is Post, HttpGet
is the default for the server-side. So you would have to explicitly say you want a HttpPost
on the server-side. Do the following steps. Note Save
method has two overloads and also one with HttpGet
and the other with HttpPost
. When you are done, put a break point on the method with the HttpPost
attribute and post the form. You will see that the model will be hydrated.
Step 1
@using (Html.BeginForm("Save", "MyEvent", FormMethod.Post))
{
<md-input-container class="md-block" flex-gt-xs="">
<label>Title</label>
@Html.TextBoxFor(m => m.title)
</md-input-container>
<md-input-container class="md-block">
<label>Address</label>
@Html.TextBoxFor(m => m.address)
</md-input-container>
<md-button class="md-raised">
<input type="submit" value="Save" />
</md-button>
}
Step 2
public class MyEventController : Controller
{
[HttpPost]
public ActionResult Save(InputEvent model)
{
// Consider refining the implementation to use Stored Procedures or an ORM e.g. Entity Framework.
// It helps secure your app. Application security advice.
MySqlConnection conn = new MySqlConnection("mydbstring");
conn.Open();
MySql.Data.MySqlClient.MySqlCommand comm = conn.CreateCommand();
comm.CommandText = "INSERT INTO event(title, address) VALUES(" + model.title + "," + model.address + ")";
comm.ExecuteNonQuery();
conn.Close();
return View();
}
[HttpGet]
public ActionResult Save()
{
return View();
}
}
Upvotes: 1
Reputation: 14133
Add the FormMethod.Post
as a third parameter... your action method is specting a Post and not a Get.
@using (Html.BeginForm("Save", "MyEvent", FormMethod.Post)){
....
}
Upvotes: 0