【发布时间】: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 错误页面未找到?
【问题讨论】:
标签: c# asp.net-mvc visual-studio model-view-controller