无法使用MVC 4从数据库中删除记录

时间:2014-06-17 12:02:19

标签: 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;
            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>
}

现在,当我每次尝试删除记录时,没有删除任何记录。我添加断点并检查值。但是在HttpPost函数中,StudentData sd没有任何单个值。它告诉我null。为什么这些属性是空的我不知道。请高手帮我解决这个错误

1 个答案:

答案 0 :(得分:1)

您的表单元素中没有任何输入要回发(注意Html.DisplayFor()方法不会呈现html输入)

@using (Html.BeginForm()) {
  @Html.EditorFor(model => model.SID)
  @Html.EditorFor(model => model.StudentName)
  // Other properties here
  <input type="submit" value="Delete" />
}

如果您不想要可编辑字段,可以为所有属性创建隐藏输入,或者只考虑回发ID并修改控制器操作