Part 24 Deleting database records using post request in mvc

preview_player
Показать описание
In this video we will discuss,
1. Deleting database records using POST request
2. Showing the client side javascript confirmation dialog box before deleting

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

Please watch Part 23, before proceeding.

Step 1: Mark "Delete" action method in "Employee" controller with [HttpPost] attribute. With this change, the "Delete" method will no longer respond to "GET" request. At this point, if we run the application and click on "Delete" link on the "Index" view, we get an error stating - "The resource cannot be found"
[HttpPost]
public ActionResult Delete(int id)
{
EmployeeBusinessLayer employeeBusinessLayer =
new EmployeeBusinessLayer();
employeeBusinessLayer.DeleteEmployee(id);
return RedirectToAction("Index");
}

REPLACE THE FOLLOWING CODE
@foreach (var item in Model) {
[tr]
[td]
@Html.DisplayFor(modelItem =] item.Name)
[/td]
[td]
@Html.DisplayFor(modelItem =] item.Gender)
[/td]
[td]
@Html.DisplayFor(modelItem =] item.City)
[/td]
[td]
@Html.DisplayFor(modelItem =] item.DateOfBirth)
[/td]
[td]
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
[/td]
[/tr]
}

WITH
@foreach (var item in Model)
{
using (Html.BeginForm("Delete", "Employee", new { id = item.ID }))
{
[tr]
[td]
@Html.DisplayFor(modelItem =] item.Name)
[/td]
[td]
@Html.DisplayFor(modelItem =] item.Gender)
[/td]
[td]
@Html.DisplayFor(modelItem =] item.City)
[/td]
[td]
@Html.DisplayFor(modelItem =] item.DateOfBirth)
[/td]
[td]
@Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
[input type="submit" value="Delete" /]
[/td]
[/tr]
}
}

Notice that, we are using "Html.BeginForm()" html helper to generate a form tag.

Step 3: To include client-side confirmation, before the data can be deleted, add the "onclick" attribute to "Delete" button as shown below.
[input type="submit" value="Delete" onclick="return confirm('Are you sure you want to delete record with ID = @item.ID');" /]

Text version of the video

Slides

All ASP .NET MVC Text Articles

All ASP .NET MVC Slides

All Dot Net and SQL Server Tutorials in English

All Dot Net and SQL Server Tutorials in Arabic
Рекомендации по теме
Комментарии
Автор

Thank you very much, i lost 1 day until i watched this video - solved my problem in 5 minutes

Regards

trezesp
Автор

Hello Venkat Sir, Your videos are just awesome!!!!

DineshGupta-xurd
Автор

That was magnificent! Just lost for words.

huslerbling
Автор

Perfect as always! No words to express my gratitude!

samirayusifova
Автор

t worked like magic. Fixed my problem. Thank You

yusufbashir
Автор

Excelent, it was very useful for me. Thanks a lot!

djoscarpino
Автор

Venkat, Great tutorials man. Thanks a lot.

PetkoPetkov
Автор

good tutorial for Beginners  ....thanks a lot ....

maryjain
Автор

Hi Sam, use onclick="return confirm('Are you sure you want to delete record with ID = @item.ID');" instead of
onclick="return confirm('Are you sure you want to delete record with ID =item.ID');". Notice the usage of "@" symbol. Please let me know if this has fixed your issue.

Csharp-video-tutorialsBlogspot
Автор

Hi, very very good question. Thanks for asking. Generally, if we have the need to use multiple models on a view, we create ViewSpecific model, which will contain data from different models. Another approach is to put those models in ViewBag or ViewData and use them in a view. If this is not clear, I will record and upload a video very soon. Hope this answers your question. Good Luck.

Csharp-video-tutorialsBlogspot
Автор

Hi Chandrashekar, wrapping the call to Html.BeginForm() in an using statement will automatically generate closing form tag. Hope this answers your question.

Csharp-video-tutorialsBlogspot
Автор

Hello Venkat Sir,

Your videos are just awesome!!!!

One question is - As you said deleting database records using get request is bad. And In This video, we are deleting record by using POST Request.

As you have shown in the video - "Part 19 - Unintended updates in mvc", We can make dummy POST Request via fiddler.
So Same we can issue such a POST Request to delete the Record.... So whats the USE then... plz plz answer it...

Eagerly waiting for reply!!!

and THANKS A TON FOR ALL YOUR It helps a LOT....

sudarshan
Автор

+kudvenkat Hello sir, You put a lot of effort for us. I am so thankful :)

In this video, I have one question. As MVC provides Html helpers for most of controls we need in application except Button control. Why MVC doesn't provide this? Is there any specific reason behind it?

Thanks again, God bless you :)

chintsbilly
Автор

using VS 2012, I got rid of all the warning/syntax error messages with small modification like this: (closing parenthesis after 'ID =') works perfect!

(less-than sign) input type="submit" value="Delete"
onclick="return confirm('Are you sure you want to delete record with ID = ') + item.ID"; (/greater-than sign)

magialatina
Автор

kudvenkat, is not it better to show the user the value of the column name? The user may feel confused about what to choose.

ranyil
Автор

Hi venkat,
what is the use of @using block here .you have wrapped entire form in it?

chandrashekarb
Автор

Sorry, where do you see the warning message? Is it on the browser or in visual studio during compilation.

Csharp-video-tutorialsBlogspot
Автор

Hallo venkat, can you please select the rows and delete the selected rows? using post request
thanks

lakshmipriya
Автор

thanks, that's what I'm looking for

malzyadi
Автор

Please Post Login Form Page creation Also.. With database credentials....

MsYagnam
join shbcf.ru