ASP.NETMVCweb应用程序-访问Edit、Details、Delete视图时出现错误404。
创始人
2024-09-19 07:30:21
0

在MVC控制器中添加以下动作方法:

public ActionResult Edit(int? id)
{
   if (id == null)
   {
      return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
   }
   YourModel model = db.YourModels.Find(id);
   if (model == null)
   {
      return HttpNotFound();
   }
   return View(model);
}

public ActionResult Details(int? id)
{
   if (id == null)
   {
      return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
   }
   YourModel model = db.YourModels.Find(id);
   if (model == null)
   {
      return HttpNotFound();
   }
   return View(model);
}

public ActionResult Delete(int? id)
{
   if (id == null)
   {
      return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
   }
   YourModel model = db.YourModels.Find(id);
   if (model == null)
   {
      return HttpNotFound();
   }
   return View(model);
}

请注意,这些动作方法的名称必须与在“控制器名称”文件夹中的视图文件的名称相同。在这个例子中,这些视图文件的名称分别是“Edit.cshtml”、“Details.cshtml”和“Delete.cshtml”。

在这些视图文件中,您需要确保表单的操作URL指向正确的控制器动作方法。例如:

@using (Html.BeginForm("Edit", "YourControllerName", FormMethod.Post))

您还需要确保在应用程序的RouteConfig.cs文件中存在适当的路由。以下是一个RouteConfig.cs文件示例,它将您的控制器名称设置为“YourControllerName”:

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 }
      );
      
      routes.MapRoute(
         name: "YourControllerName",
         url: "YourControllerName

相关内容

热门资讯

推荐一款!手机透视辅助器app... 推荐一款!手机透视辅助器app,爱玩娱乐暗堡辅助,2026新版(有挂神器)-哔哩哔哩 了解更多开挂安...
必备攻略!皮皮胡子辅助,hhp... 必备攻略!皮皮胡子辅助,hhpoker辅助是真的吗,解密教程(今日头条)-哔哩哔哩;打开点击测试直接...
分享认知!杭州都莱辅助软件下载... 分享认知!杭州都莱辅助软件下载,红龙poker辅助工具,微扑克教程(有挂秘笈)-哔哩哔哩;无需打开直...
必看攻略!欢乐茶馆挂,德州hh... 必看攻略!欢乐茶馆挂,德州hhpoker是真的吗,黑科技教程(有挂头条)-哔哩哔哩 【无需打开直接搜...
推荐十款!陕西扣点点脚本辅助器... 推荐十款!陕西扣点点脚本辅助器,创思维激k看底牌辅助,靠谱教程(有挂秘诀)-哔哩哔哩【无需打开直接搜...
透视好牌!陕麻圈破解,hhpo... 透视好牌!陕麻圈破解,hhpoker辅助靠谱吗,透明挂教程!(有挂头条)-哔哩哔哩;亲,陕麻圈破解这...
记者爆料!微乐自建房怎么提高胜... 微乐自建房怎么提高胜率是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用户可以...
总算明白!上饶打炸作弊,德州局... 上饶打炸作弊开挂教程视频分享装挂详细步骤在当今的网络游戏中,上饶打炸作弊作为一种经典的娱乐方式,吸引...
重大通报!四川微乐自建房辅助器... 重大通报!四川微乐自建房辅助器,杭州都莱破解版,玩家教你(有挂技巧)-哔哩哔哩;无需打开直接搜索打开...
今日头条!填大坑辅助视频,广东... 较多好评“微乐万能挂官网”开挂(透视)辅助教程 了解更多开挂安装加(136704302)微信号是一款...