尝试将新对象添加到列表时出现 404 错误 MVC
Getting 404 Error MVC when trying to add New Object to a List
我有朋友视图,我的朋友列在 table 中,如下图所示。
现在我添加了新功能添加朋友所以当我点击 Link 我得到:
The resource cannot be found. 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 错误 找不到页面?
问题是您的 AddNewFriend
视图没有重定向的操作,视图应该有一个带有方法 get
的操作来重定向,并且 post
方法是一次您在视图中提交表单
在您的控制器中添加以下方法应该可以解决问题
[HttpGet]
public ActionResult AddNewFriend()
{
return View();
}
错误 404 表示无法从您的 URL.
中找到路线
在您的 Razor 页面中检查这一行
@Html.ActionLink("Edit", "EditFriend", new { id = friend.Id }, null)
你说的是,当我单击 link 时,转到 controller:"Edit" & action:"EditFriend"
。
这将解析为“https:///localhost/Edit/EditFriend?id=1”
但我在这里找不到 EditController,因此 link 创建了 404
试着把“Friend
”放在这里,因为我们有一个“FriendController
”。喜欢
@Html.ActionLink("Friend", "EditFriend", new { id = friend.Id }, null)
这将解析为“https://localhost/Friend/EditFriend?id=1”
我有朋友视图,我的朋友列在 table 中,如下图所示。
现在我添加了新功能添加朋友所以当我点击 Link 我得到:
The resource cannot be found. 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 错误 找不到页面?
问题是您的 AddNewFriend
视图没有重定向的操作,视图应该有一个带有方法 get
的操作来重定向,并且 post
方法是一次您在视图中提交表单
在您的控制器中添加以下方法应该可以解决问题
[HttpGet]
public ActionResult AddNewFriend()
{
return View();
}
错误 404 表示无法从您的 URL.
中找到路线在您的 Razor 页面中检查这一行
@Html.ActionLink("Edit", "EditFriend", new { id = friend.Id }, null)
你说的是,当我单击 link 时,转到 controller:"Edit" & action:"EditFriend"
。
这将解析为“https:///localhost/Edit/EditFriend?id=1”
但我在这里找不到 EditController,因此 link 创建了 404
试着把“Friend
”放在这里,因为我们有一个“FriendController
”。喜欢
@Html.ActionLink("Friend", "EditFriend", new { id = friend.Id }, null)
这将解析为“https://localhost/Friend/EditFriend?id=1”