Reputation: 167
I am trying to delete data using MVC in my application. For this first I am fetching the records from the database into the labels. Then I am trying to delete this. The code which I am writing is below
This is the code of my Controller
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();
}
}
Here is my model
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();
}
}
as you see StudentData is a class in which I define properties.
Now here is my View
@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>
}
Now when I am trying to delete my record everytime,not a single record was deleted .I add breakpoint and check the values. But In HttpPost function StudentData sd had not any single value. Its show me null.Why these properties are empty I dnt know. Please experts Help me to get out of this error
Upvotes: 1
Views: 1854
Reputation:
You do not have any inputs within you form element to post back (note Html.DisplayFor()
methods do not render html inputs)
@using (Html.BeginForm()) {
@Html.EditorFor(model => model.SID)
@Html.EditorFor(model => model.StudentName)
// Other properties here
<input type="submit" value="Delete" />
}
If you don't want editable fields you can create hidden inputs for all properties or consider just posting back the ID and modifying you controller action
Upvotes: 1