gpt4 book ai didi

c# - 如何将 HTML 元素的值分配为 ASP.NET 中 MVC 模态类中定义的变量的值?

转载 作者:行者123 更新时间:2023-11-27 23:24:36 25 4
gpt4 key购买 nike

我正在使用 ADO.Net 探索 MVC 中的 CRUD 操作,以便存储员工记录并根据需要对其进行操作。当我在模型中定义表列时,MVC 相应地生成 View ,该 View 生成 HTML 帮助程序而不是普通的 html 元素。所以现在我有了这个 html 元素:
<input type="text" id="datepicker">
此文本框从日期选择器 jQuery API 获取一串日期时间。现在我在 View 模型中将其定义为

   [Display(Name ="Travel Days")]
public string Date { get; set; }

现在我想执行一个操作,将值Date 传递给数据库。为此,我在这里执行了某些操作:

public bool AddEmpInfo(EmpInfoModels Info)
{
using (connect = new SqlConnection(ConfigurationManager.ConnectionStrings["CrudMvc"].ToString()))
{

SqlCommand com = new SqlCommand("AddEmpInfo", connect);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@Date", Info.Date);
connect.Open();
int i = com.ExecuteNonQuery();
if (i >0)
{
return true;
}
else
{
return false;
}
}
}


当我调试该过程时,从 View 类提交的值未随 Info.Date 一起接收。

所以我的问题是绑定(bind)从纯 html 元素输入的值并将其作为参数发送到存储过程以执行数据库操作。

最佳答案

添加名称与模型绑定(bind)

<input type="text" id="datepicker" name="Date">

关于c# - 如何将 HTML 元素的值分配为 ASP.NET 中 MVC 模态类中定义的变量的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39224604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com