How to make a submit button with MVC 4
asp.net-mvc, asp.net-mvc-4
Solution
In MVC you have to create a form and submit that form to the Controller's Action method. The syntax for creating form given as:
View:
@using (Html.BeginForm("YourActionName", "ControllerName"))
{
@Html.TextBoxFor(m => m.FirstName)
@Html.TextBoxFor(m => m.LastName)
<input type="submit" value="Submit Data" id="btnSubmit" />
}
Controller:
public ActionResult YourActionName(UserModel model)
{
//some operations goes here
return View(); //return some view to the user
}
Model:
public class UserModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
Problem
I am trying to write a code that takes the last name and first name from user input and stores the values in a data table with MVC4. I have added the following code under Accountcontroller.cs that will create a submit button. Once the user clicks the submit button it would add the user input to the data set. ``` private void button_Click( object sender, EventArgs e) { SqlConnection cs = new SqlConnection("Data Source = FSCOPEL-PC; .... SqlDataAdapter da = new SqlDataAdapter(); da.insertCommand = new SqlCommand(" INSERT INTO TABLE VALUES ( Firstname, Lastname, ) } ``` I have also added the following code under logincs.html that will create the submit button, once the user logins. ``` <button type="submit" id="btnSave" name="Command" value="Save">Save</button> ```