TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Tri Setia
1.3k
464
24.4k
CRUD using ASP.NET MVC
Apr 2 2021 4:34 AM
Hai guys,, l trying to learn CRUD in Vs 2019 Mvc Aplication. I Get error in Update and Delete Record.
the id taken properly in the Url but the data not loaded on view
AddOrEdit.cshtml.
I'm not use EF for the CRUD operation.
I'm get error like this when the edit is click.
the error when delete click
this the controller
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Threading.Tasks;
using
Microsoft.AspNetCore.Mvc;
using
Microsoft.AspNetCore.Mvc.Rendering;
using
Microsoft.EntityFrameworkCore;
using
WebApplicationMvc.Data;
using
WebApplicationMvc.Models;
using
Microsoft.Extensions.Configuration;
using
System.Data;
using
System.Data.SqlClient;
using
Microsoft.Data.SqlClient;
namespace
WebApplicationMvc.Controllers
{
public
class
GuestBookController : Controller
{
private
readonly
IConfiguration _configuration;
public
GuestBookController(IConfiguration configuration)
{
this
._configuration = configuration;
}
// GET: GuestBook
public
IActionResult Index()
{
DataTable dt =
new
DataTable();
using
(SqlConnection con =
new
SqlConnection(_configuration.GetConnectionString(
"db_mvc"
)))
{
con.Open();
SqlCommand cmd =
new
SqlCommand(
"spGetAllGuestBook"
, con);
SqlDataAdapter adap =
new
SqlDataAdapter(cmd);
cmd.CommandType = CommandType.StoredProcedure;
adap.Fill(dt);
}
return
View(dt);
}
// GET: GuestBook/AddOrEdit/
public
IActionResult AddOrEdit(
int
? id)
{
GuestBookViewModel guestBookViewModel =
new
GuestBookViewModel();
if
(id > 0)
guestBookViewModel = GetGuestBookByID(id);
return
View(guestBookViewModel);
}
// POST: GuestBook/AddOrEdit
// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public
IActionResult AddOrEdit(
int
id, [Bind(
"Id,Name,Email,Message"
)] GuestBookViewModel guestBookViewModel)
{
if
(ModelState.IsValid)
{
using
(SqlConnection con =
new
SqlConnection(_configuration.GetConnectionString(
"db_mvc"
)))
{
con.Open();
SqlCommand cmd =
new
SqlCommand(
"spSaveOrEdit"
, con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue(
"Id"
, guestBookViewModel.Id);
cmd.Parameters.AddWithValue(
"Name"
, guestBookViewModel.Name);
cmd.Parameters.AddWithValue(
"Email"
, guestBookViewModel.Email);
cmd.Parameters.AddWithValue(
"Message"
, guestBookViewModel.Message);
cmd.ExecuteNonQuery();
}
return
RedirectToAction(nameof(Index));
}
return
View();
}
// GET: GuestBook/Delete/5
public
IActionResult Delete(
int
? id)
{
GuestBookViewModel guestBookViewModel =
new
GuestBookViewModel();
guestBookViewModel = GetGuestBookByID(id);
return
View(guestBookViewModel);
}
// POST: GuestBook/Delete/5
[HttpPost, ActionName(
"Delete"
)]
[ValidateAntiForgeryToken]
public
IActionResult DeleteConfirmed(
int
id)
{
using
(SqlConnection con =
new
SqlConnection(_configuration.GetConnectionString(
"db_mvc"
)))
{
con.Open();
SqlCommand cmd =
new
SqlCommand(
"spDeleteGuestBookByID"
, con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue(
"Id"
, id);
cmd.ExecuteNonQuery();
return
RedirectToAction(nameof(Index));
}
}
[NonAction]
public
GuestBookViewModel GetGuestBookByID(
int
? id)
{
GuestBookViewModel guestBookViewModel =
new
GuestBookViewModel();
using
(SqlConnection con =
new
SqlConnection(_configuration.GetConnectionString(
"db_mvc"
)))
{
DataTable dt =
new
DataTable();
con.Open();
SqlDataAdapter adap =
new
SqlDataAdapter(
"spGetGuestBookByID"
, con);
adap.SelectCommand.CommandType = CommandType.StoredProcedure;
adap.SelectCommand.Parameters.AddWithValue(
"@Id"
, id);
adap.Fill(dt);
if
(dt.Rows.Count == 1)
{
guestBookViewModel.Id = Convert.ToInt32(dt.Rows[0][
"Id"
].ToString());
guestBookViewModel.Name = dt.Rows[1][
"Name"
].ToString();
guestBookViewModel.Email = dt.Rows[2][
"Email"
].ToString();
guestBookViewModel.Message = dt.Rows[3][
"Message"
].ToString();
}
return
guestBookViewModel;
}
}
}
}
this the index.cshtml
@model System.Data.DataTable
@{
ViewData[
"Title"
] =
"List Guest Book"
;
}
<div
class
=
"jumbotron bg-info"
>
<h1
class
=
"text-center"
>@ViewData[
"Title"
]</h1>
</div>
<p>
<div
class
=
"btn btn-light"
>
<a asp-action=
"AddOrEdit"
>Create New</a>
</div>
</p>
<table
class
=
"table table-bordered"
>
<thead
class
=
"thead-light text-center"
>
<tr>
<th>
Name
</th>
<th>
Email
</th>
<th>
Message
</th>
<th>
</th>
</tr>
</thead>
<tbody>
@
for
(
int
i = 0; i < Model.Rows.Count; i++)
{
<tr>
<td>
@Model.Rows[i][
"Name"
]
</td>
<td>
@Model.Rows[i][
"Email"
]
</td>
<td>
@Model.Rows[i][
"Message"
]
</td>
<td>
<a asp-action=
"AddOrEdit"
asp-route-id=
"@Model.Rows[i]["
Id
"]"
>Edit</a> |
<a asp-action=
"Delete"
asp-route-id=
"@Model.Rows[i]["
Id
"]"
>Delete</a>
</td>
</tr>
}
</tbody>
</table>
this is the AddOrEdit.cshtml
@model WebApplicationMvc.Models.GuestBookViewModel
@{
ViewData[
"Title"
] = Model.Id==0?
"Save Data"
:
"Update Data"
;
}
<h1>@ViewData[
"Title"
]</h1>
<hr />
<div
class
=
"row"
>
<div
class
=
"col-md-4"
>
<form asp-action=
"AddOrEdit"
autocomplete=
"off"
>
<div asp-validation-summary=
"ModelOnly"
class
=
"text-danger"
></div>
<input type=
"hidden"
asp-
for
=
"Id"
/>
<div
class
=
"form-group"
>
<label asp-
for
=
"Name"
class
=
"control-label"
></label>
<input asp-
for
=
"Name"
class
=
"form-control"
/>
<span asp-validation-
for
=
"Name"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group"
>
<label asp-
for
=
"Email"
class
=
"control-label"
></label>
<input asp-
for
=
"Email"
class
=
"form-control"
/>
<span asp-validation-
for
=
"Email"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group"
>
<label asp-
for
=
"Message"
class
=
"control-label"
></label>
<input asp-
for
=
"Message"
class
=
"form-control"
/>
<span asp-validation-
for
=
"Message"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group"
>
<input type=
"submit"
value=
"Save Data"
class
=
"btn btn-primary"
/>
</div>
</form>
</div>
</div>
<div>
<a asp-action=
"Index"
>Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync(
"_ValidationScriptsPartial"
);}
}
this is the delete.cshtml
@model WebApplicationMvc.Models.GuestBookViewModel
@{
ViewData[
"Title"
] =
"Delete Guest Book"
;
}
<h1>@ViewData[
"Title"
]</h1>
<h3
class
=
"alert-danger"
>Are you sure you want to
delete
this
?</h3>
<div>
<h4>GuestBookViewModel</h4>
<hr />
<dl
class
=
"row"
>
<dt
class
=
"col-sm-2"
>
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd
class
=
"col-sm-10"
>
@Html.DisplayFor(model => model.Name)
</dd>
<dt
class
=
"col-sm-2"
>
@Html.DisplayNameFor(model => model.Email)
</dt>
<dd
class
=
"col-sm-10"
>
@Html.DisplayFor(model => model.Email)
</dd>
<dt
class
=
"col-sm-2"
>
@Html.DisplayNameFor(model => model.Message)
</dt>
<dd
class
=
"col-sm-10"
>
@Html.DisplayFor(model => model.Message)
</dd>
</dl>
<form asp-action=
"Delete"
>
<input type=
"hidden"
asp-
for
=
"Id"
/>
<input type=
"submit"
value=
"Delete"
class
=
"btn btn-danger"
/> |
<a asp-action=
"Index"
>Back to List</a>
</form>
</div>
Reply
Answers (
5
)
How to remain the treeview after redirect to another page?
Convert Rows to Columns using C# logic with entity framework