尝试将新对象添加到列表时出现 404 错误 MVC
Posted
技术标签:
【中文标题】尝试将新对象添加到列表时出现 404 错误 MVC【英文标题】:Getting 404 Error MVC when trying to add New Object to a List 【发布时间】:2021-07-29 01:57:09 【问题描述】:我有朋友视图,我的朋友在哪里列出,如下图所示。
现在我添加了新功能“添加朋友”,所以当我点击我得到的链接时:
找不到资源。 HTTP 404
这是我列出所有朋友的视图:
@using Lab3.Models
@model IEnumerable<Lab3.Models.FriendModel>
@
ViewBag.Title = "Friends";
Layout = "~/Views/Shared/_Layout.cshtml";
<h2>Friends</h2>
@Html.ActionLink("Add Friend", "AddNewFriend", "Friend", null, new @class = "btn btn-primary" )
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Friend Id</th>
<th>Friend Name</th>
<th>Place</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (FriendModel friend in Model)
<tr>
<td>@friend.Id</td>
<td>@friend.Ime</td>
<td>@friend.MestoZiveenje</td>
<td>
@Html.ActionLink("Edit", "EditFriend", new id = friend.Id , null)
</td>
</tr>
</tbody>
</table>
AddFriend
视图:
@model Lab3.Models.FriendModel
@
ViewBag.Title = "AddFriend";
Layout = "~/Views/Shared/_Layout.cshtml";
<h2>AddFriend</h2>
@using (Html.BeginForm("AddNewFriend","Friend"))
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>FriendModel</h4>
<hr />
@Html.ValidationSummary(true, "", new @class = "text-danger" )
<div class="form-group">
@Html.LabelFor(model => model.Ime, htmlAttributes: new @class = "control-label col-md-2" )
<div class="col-md-10">
@Html.EditorFor(model => model.Ime, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.Ime, "", new @class = "text-danger" )
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.MestoZiveenje, htmlAttributes: new @class = "control-label col-md-2" )
<div class="col-md-10">
@Html.EditorFor(model => model.MestoZiveenje, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.MestoZiveenje, "", new @class = "text-danger" )
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
<div>
@Html.ActionLink("Back to List", "Index")
</div>
还有我的FriendController
:
using Lab3.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Lab3.Controllers
public class FriendController : Controller
private static List<FriendModel> friendModels;
// GET: Friend
public ViewResult Index()
var friends = GetFriends();
return View(friends);
public ViewResult EditFriend(byte id)
var friend = GetFriends().SingleOrDefault(f => f.Id == id);
return View("EditFriend",friend);
[HttpPost]
public ActionResult AddNewFriend(FriendModel friend)
friendModels.Add(friend);
return View("Index", friendModels);
private IEnumerable<FriendModel> GetFriends()
return new List<FriendModel>
new FriendModel Id = 1, Ime = "Marry", MestoZiveenje = "Dubai",
new FriendModel Id = 2, Ime = "John", MestoZiveenje = "London",
new FriendModel Id = 3, Ime = "Smith", MestoZiveenje = "Manchester"
;
为什么我会收到此404 错误页面未找到?
【问题讨论】:
【参考方案1】:问题是你的AddNewFriend
View 没有重定向的动作,一个视图应该有一个带有方法get
的动作来重定向,post
方法适用于你在视图中提交表单后
在您的控制器中添加以下方法应该可以解决问题
[HttpGet]
public ActionResult AddNewFriend()
return View();
【讨论】:
【参考方案2】:错误 404 表示它无法从您的 URL 中找到路由。
在你的 Razor 页面中检查这一行
@Html.ActionLink("Edit", "EditFriend", new id = friend.Id , null)
您在这里告诉我,当我单击链接时,转到 controller:"Edit" & action:"EditFriend"
。
这将解析为“https://localhost/Edit/EditFriend?id=1”
但我在这里找不到 EditController,因此此链接会创建 404
尝试将“Friend
”放在这里,因为我们有一个“FriendController
”。喜欢
@Html.ActionLink("Friend", "EditFriend", new id = friend.Id , null)
这将解析为“https://localhost/Friend/EditFriend?id=1”
【讨论】:
以上是关于尝试将新对象添加到列表时出现 404 错误 MVC的主要内容,如果未能解决你的问题,请参考以下文章