2

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?

7
  • I'm not sure how your routing is set up, but I think I would expect to see Html.BeginForm("Save", "MyEvent") instead of the explicit controller path. What does the browser show in the dev console when submitting the form? Commented Mar 20, 2016 at 0:07
  • Watch your browser's network monitor. Are the requests successful? Commented Mar 20, 2016 at 0:08
  • when i change it to just "MyEvent" instead of "Portal/Controllers/MyEvent" it tries to take me to the URL: "MyEvent/Save". I don't think that is what i want. The console has no errors, it just says: Navigated to localhost:8000/App/Add_Event Commented Mar 20, 2016 at 0:10
  • You need to replace "Portal/Controllers/MyEvent" with the actual name of the controller - i.e. "MyEvent" Commented Mar 20, 2016 at 0:11
  • All requests in the network monitor are successful. Commented Mar 20, 2016 at 0:25

3 Answers 3

1

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();
     }
 }
Sign up to request clarification or add additional context in comments.

2 Comments

@ Stephen Muecke , I used the word "Ensure", because @user3329046 could have it here on StackOverflow and not in source code.
@JuliusDepulla It's pretty obvious he has got it when he says "my controller". It's just copy and pasted from his source code.
0

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)){
  ....
}

2 Comments

The default is FormMethod.Post - this is not necessary at all (refer overload)
As Stephen said, i'm pretty sure FormMethod.Post is default. i tried adding it and nothing changed.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.