例如,当我输入JSBN时显示但是我输入错误键入其他地址,例如http://localhost:2430/newss/Create或删除。 我的查看代码是正确的,没问题。 给出以下错误: 您要查找的资源已被删除,名称已更改或暂时不可用。
@model MvcApplication17.Models.NEWS
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>NEWS</legend>
@Html.HiddenFor(model => model.NEWSID)
<div class="editor-label">
@Html.LabelFor(model => model.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Title)
@Html.ValidationMessageFor(model => model.Title)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Text)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Text)
@Html.ValidationMessageFor(model => model.Text)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.InsertDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.InsertDate)
@Html.ValidationMessageFor(model => model.InsertDate)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.GroupingID, "Subjects")
</div>
<div class="editor-field">
@Html.DropDownList("GroupingID", String.Empty)
@Html.ValidationMessageFor(model => model.GroupingID)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
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 }
);
}
控制器代码:
[HttpPost]
public ActionResult Edit(NEWS news)
{
if (ModelState.IsValid)
{
db.Entry(news).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.GroupingID = new SelectList(db.Subjectss, "GroupingID", "Farsi", news.GroupingID);
return View(news);
}
答案 0 :(得分:0)
您发布的错误表示您尝试访问的链接存在问题。
也许您没有能够处理该网址的控制器,或者您可能遇到路由问题。
好的,请在你的Route.Config中试试这个:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//Route defined to manage /newss/Edit/news
routes.MapRoute(
name: "Edit",
url: "newss/Edit/{news}",
defaults: new { controller = "newss", action = "Edit", news = new NEWS() }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
答案 1 :(得分:0)
在MVC中,URL与视图的物理位置不对应。相反,根据您的路由配置,URL映射到特定控制器中的特定操作,然后,基于显式引用的约定,使用视图。
换句话说,如果您获得该URL的404,则表示没有匹配的路由,或者没有映射到该路由的控制器/操作组合。它与你所拥有的观点无关。