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

我正在尝试编写一个代码,该代码用户输入中获取姓氏和名字,并将值存储在带有MVC4的数据表中.我在Accountcontroller.cs下添加了以下代码

这将创建一个提交按钮.一旦用户单击提交按钮,它就会将用户输入添加到数据集中.

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,)
}

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

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

解决方法

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

视图:

@using (Html.BeginForm("YourActionName","ControllerName"))
{
    @Html.TextBoxFor(m => m.FirstName)
    @Html.TextBoxFor(m => m.LastName)
    <input type="submit" value="Submit Data" id="btnSubmit" />
}

控制器:

public ActionResult YourActionName(usermodel model)
       {
          //some operations goes here
          return View(); //return some view to the user
       }

模型:

public class usermodel
{
   public string FirstName { get; set; }
   public string LastName { get; set; }
}

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....