asp.net-mvc 如何使用 MVC 4 制作提交按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21659771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:31:35  来源:igfitidea点击:

How to make a submit button with MVC 4

asp.net-mvcasp.net-mvc-4

提问by demonslayer1

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

我正在尝试编写一个代码,该代码从用户输入中获取姓氏和名字,并将值存储在带有 MVC4 的数据表中。我在 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.

我还在 logincs.html 下添加了以下代码,一旦用户登录,它将创建提交按钮。

   <button type="submit" id="btnSave" name="Command" value="Save">Save</button>

回答by Bhupendra Shukla

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:

在 MVC 中,您必须创建一个表单并将该表单提交给 Controller 的 Action 方法。创建表单的语法如下:

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; }
}