Asp.net mvc 4 无法使用MVC 4从数据库中删除记录
我正在尝试在我的应用程序中使用MVC删除数据。首先,我将把数据库中的记录提取到标签中。然后我试图删除这个。下面是我正在写的代码 这是我的控制器的代码Asp.net mvc 4 无法使用MVC 4从数据库中删除记录,asp.net-mvc-4,c#-4.0,ado.net,Asp.net Mvc 4,C# 4.0,Ado.net,我正在尝试在我的应用程序中使用MVC删除数据。首先,我将把数据库中的记录提取到标签中。然后我试图删除这个。下面是我正在写的代码 这是我的控制器的代码 public ActionResult Delete(string id) { DBData dbData = new DBData(); StudentData sd = new StudentData(); DataSet ds; d
public ActionResult Delete(string id)
{
DBData dbData = new DBData();
StudentData sd = new StudentData();
DataSet ds;
ds = dbData.SelectUserById(id);
sd.SID = Convert.ToInt32(ds.Tables[0].Rows[0]["sid"].ToString());
sd.StudentName = ds.Tables[0].Rows[0]["studentname"].ToString();
sd.ContactNo = ds.Tables[0].Rows[0]["contactno"].ToString();
sd.EmailId = ds.Tables[0].Rows[0]["emailid"].ToString();
return View(sd);
}
[HttpPost]
public ActionResult Delete(StudentData sd)
{
if (ModelState.IsValid)
{
DBData db = new DBData();
db.DeleteRecord(sd);
return RedirectToAction("SelectAllStudent", "Student");
}
else
{
ModelState.AddModelError("", "Error while Deleting data");
return View();
}
}
这是我的模型
public void DeleteRecord(StudentData sd)
{
SqlConnection con = new SqlConnection();
try
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("delete from studentinfo where sid=@sid", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@sid", sd.SID);
cmd.ExecuteNonQuery();
cmd.Dispose();
}
catch
{
}
finally
{
con.Close();
}
}
正如您所看到的,StudentData是一个我定义属性的类
这是我的看法
@model CrudMVC.Models.StudentData
@{
ViewBag.Title = "Delete";
}
<h2>Delete</h2>
<h3>Are you sure you want to delete this?</h3>
<fieldset>
<legend>StudentData</legend>
<div class="display-label">
@Html.DisplayNameFor(model => model.SID)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.SID)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.StudentName)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.StudentName)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.EmailId)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.EmailId)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.ContactNo)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.ContactNo)
</div>
</fieldset>
@using (Html.BeginForm()) {
<p>
<input type="submit" value="Delete" /> |
@Html.ActionLink("Back to List", "SelectAllStudent")
</p>
}
@model CrudMVC.Models.StudentData
@{
ViewBag.Title=“删除”;
}
删除
是否确实要删除此项?
学生数据
@DisplayNameFor(model=>model.SID)
@DisplayFor(model=>model.SID)
@DisplayNameFor(model=>model.StudentName)
@DisplayFor(model=>model.StudentName)
@DisplayNameFor(model=>model.EmailId)
@DisplayFor(model=>model.EmailId)
@DisplayNameFor(model=>model.ContactNo)
@DisplayFor(model=>model.ContactNo)
@使用(Html.BeginForm()){
|
@ActionLink(“返回列表”,“选择所有学生”)
}
现在,当我每次试图删除我的记录时,没有一条记录被删除。我添加断点并检查值。但在HttpPost函数中,StudentData sd没有任何单一值。我不知道为什么这些属性是空的。请专家帮助我摆脱此错误您的表单元素中没有任何要发回的输入(注意
Html.DisplayFor()
方法不呈现Html输入)
@使用(Html.BeginForm()){
@EditorFor(model=>model.SID)
@EditorFor(model=>model.StudentName)
//这里还有其他物业吗
}
如果你不想编辑字段,你可以为所有属性创建隐藏的输入,或者考虑只返回ID并修改你的控制器动作
使用(html。{在
的顶部,然后再试一次。我仍然不认为这会起作用,因为所有字段都是标签,不会发布到服务器上。尝试在表单标签中添加隐藏字段。同样,对于删除,你不需要整个对象,只需要密钥就可以了。隐藏字段技巧很有效。我在隐藏字段中添加了sid。但我不理解w为什么会发生这种情况?你知道@nilesh吗检查@Stephen的答案。DisplayFor
生成的标签不是输入元素。当你提交表单时,只有输入字段会用它们的当前值发回。因此,在你的情况下,你期望一个StudentModel
作为你的操作的输入,当你渲染时,它永远不会起作用r.同样,当删除一条记录时,您需要的只是要删除的记录的Id,因此您可以将Delete(StudentModel student)
方法更改为Delete(int studentId)
这应该足够了。是的,谢谢Nilesh,它现在运行得很好:)。我是MVC的新手。尝试从asp.net过渡到MVC。我希望有一天我会很快学会MVC:)@NileshYup你会让在MVC中开发应用程序变得非常有趣。不过,我会反过来,从MVC过渡到asp.net。:)okk的意思是@Html.DisplayFor不起作用对于任何类型的编辑、删除和更新?我是对的吗?是的,这是正确的。对于回发,您需要
(或
)元素。谢谢stephen,现在它工作正常。但是我是MVC的新手。您能给我一些好的参考资料来学习更多吗?也许从
@using (Html.BeginForm()) {
@Html.EditorFor(model => model.SID)
@Html.EditorFor(model => model.StudentName)
// Other properties here
<input type="submit" value="Delete" />
}