Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a submit button with MVC 4

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>
like image 346
demonslayer1 Avatar asked Feb 09 '14 13:02

demonslayer1


People also ask

How many ways are there to submit a form in asp net MVC?

We examine four ways to create forms in ASP.NET MVC 5: weakly typed synchronous forms, strongly typed synchronous forms, strongly typed asynchronous forms, and using jQuery AJAX to post form data.

What is HTML ActionLink in MVC?

Html. ActionLink creates a hyperlink on a view page and the user clicks it to navigate to a new URL. It does not link to a view directly, rather it links to a controller's action.


1 Answers

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; }
}
like image 135
Bhupendra Shukla Avatar answered Oct 16 '22 05:10

Bhupendra Shukla