为每个产品制作一个带有其 ID 的页面
Making a page for every product with it's ID
我的问题是我想要用最简单的方法为每个产品创建一个页面,其中包含之前存储在数据库中的 ID
示例:
Home/Products/"The product ID in the database"
而这个link必须为产品显示"Details"^
目的是当我添加新产品时,会自动创建一个新页面,其中包含产品 ID。
控制器中的动作代码:
[HttpPost]
public ActionResult AddArticle(NewsData art)
{
var ArticleID = art.ArticleID;
using (MatrodyEntities db = new MatrodyEntities())
{
db.NewsData.Add(art);
db.SaveChanges();
}
return View(art);
}
路由配置:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
好的,所以为了根据参数为 CRUD 操作创建 link,您可以使用 AJAX 到 POST 带参数的数据,或者您可以使用 Html.ActionLink
而不是 POST 而是创建锚标记。
1) 使用带参数的 ActionLink:
@foreach (var item in Model)
{
<tr>
//Additional data
<td>
@Html.ActionLink("Delete", "Home", new { id = item.id})
</td>
</tr>
}
在您的 Home
控制器中:
public ActionResult Delete(int id) {//Your logic}
请注意,如果您使用的是默认值 RouteConfig
,请确保将 id
作为参数发送,或者您可以根据需要创建自己的路由。
2) 您也可以使用 AJAX 到 POST 到您的控制器。你可以这样做:
@foreach (var item in Model)
{
<tr>
//Additional data
<td>
<a href="#" data-id="@item.id" onclick="confirmDelete(this)"></a>
</td>
</tr>
}
在你的AJAX中:
function confirmDelete(event) {
var recordToDelete = $(event).attr("data-id"); //Get our current file id here
if (confirm("Are you sure you want to delete this record") == true) {
//Prepare our data
var json = {
id: recordToDelete
};
$.ajax({
url: '@Url.Action("DeleteFile", "Home")',
type: "POST",
dataType: "json",
data: { "json": JSON.stringify(json) },
success: function (data) {
if(data == "success") {
alert("Successfully deleted selected file");
location.reload();
}
},
error: function (data) {
alert("Could not delete selected file. Please try again!");
},
});
}
};
最后在您的控制器中:
//Delete a file based on the ID that you get from your View
[HttpPost]
public JsonResult DeleteFile(string json)
{
var serializer = new JavaScriptSerializer();
try
{
dynamic jsondata = serializer.Deserialize(json, typeof(object));
string id = jsondata["id"];
if(id != "")
{
int getid = Convert.ToInt32(id);
//Call your db or your logic to delete the file
DatabaseAccess data = new DatabaseAccess();
string result = data.DeleteFile(getid);
if(result.Equals("Success"))
{
return Json("success", JsonRequestBehavior.AllowGet);
}
else
{
return Json("fail", JsonRequestBehavior.AllowGet);
}
}
else
{
return Json("notfound", JsonRequestBehavior.AllowGet);
}
}
catch
{
return Json("dberror", JsonRequestBehavior.AllowGet);
}
}
我的问题是我想要用最简单的方法为每个产品创建一个页面,其中包含之前存储在数据库中的 ID
示例:
Home/Products/"The product ID in the database"
而这个link必须为产品显示"Details"^
目的是当我添加新产品时,会自动创建一个新页面,其中包含产品 ID。
控制器中的动作代码:
[HttpPost]
public ActionResult AddArticle(NewsData art)
{
var ArticleID = art.ArticleID;
using (MatrodyEntities db = new MatrodyEntities())
{
db.NewsData.Add(art);
db.SaveChanges();
}
return View(art);
}
路由配置:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
好的,所以为了根据参数为 CRUD 操作创建 link,您可以使用 AJAX 到 POST 带参数的数据,或者您可以使用 Html.ActionLink
而不是 POST 而是创建锚标记。
1) 使用带参数的 ActionLink:
@foreach (var item in Model)
{
<tr>
//Additional data
<td>
@Html.ActionLink("Delete", "Home", new { id = item.id})
</td>
</tr>
}
在您的 Home
控制器中:
public ActionResult Delete(int id) {//Your logic}
请注意,如果您使用的是默认值 RouteConfig
,请确保将 id
作为参数发送,或者您可以根据需要创建自己的路由。
2) 您也可以使用 AJAX 到 POST 到您的控制器。你可以这样做:
@foreach (var item in Model)
{
<tr>
//Additional data
<td>
<a href="#" data-id="@item.id" onclick="confirmDelete(this)"></a>
</td>
</tr>
}
在你的AJAX中:
function confirmDelete(event) {
var recordToDelete = $(event).attr("data-id"); //Get our current file id here
if (confirm("Are you sure you want to delete this record") == true) {
//Prepare our data
var json = {
id: recordToDelete
};
$.ajax({
url: '@Url.Action("DeleteFile", "Home")',
type: "POST",
dataType: "json",
data: { "json": JSON.stringify(json) },
success: function (data) {
if(data == "success") {
alert("Successfully deleted selected file");
location.reload();
}
},
error: function (data) {
alert("Could not delete selected file. Please try again!");
},
});
}
};
最后在您的控制器中:
//Delete a file based on the ID that you get from your View
[HttpPost]
public JsonResult DeleteFile(string json)
{
var serializer = new JavaScriptSerializer();
try
{
dynamic jsondata = serializer.Deserialize(json, typeof(object));
string id = jsondata["id"];
if(id != "")
{
int getid = Convert.ToInt32(id);
//Call your db or your logic to delete the file
DatabaseAccess data = new DatabaseAccess();
string result = data.DeleteFile(getid);
if(result.Equals("Success"))
{
return Json("success", JsonRequestBehavior.AllowGet);
}
else
{
return Json("fail", JsonRequestBehavior.AllowGet);
}
}
else
{
return Json("notfound", JsonRequestBehavior.AllowGet);
}
}
catch
{
return Json("dberror", JsonRequestBehavior.AllowGet);
}
}