MVC4-Ajax.ActionLink找不到控制器方法。我做错了什么
这可能是一个非常基本的问题,但出于某种原因,我的链接无法找到控制器和方法 我在一个名为_PartialTeams.cshtml的partialview中有这个Ajax.ActionLinkMVC4-Ajax.ActionLink找不到控制器方法。我做错了什么,ajax,asp.net-mvc-4,hyperlink,Ajax,Asp.net Mvc 4,Hyperlink,这可能是一个非常基本的问题,但出于某种原因,我的链接无法找到控制器和方法 我在一个名为_PartialTeams.cshtml的partialview中有这个Ajax.ActionLink @model Website.Models.modelTeamSelect <div id="divCreatedTeams" style="float: left; padding: 10px;"> <h3>Your created teams:</h
@model Website.Models.modelTeamSelect
<div id="divCreatedTeams" style="float: left; padding: 10px;">
<h3>Your created teams:</h3>
<input type="submit" value="Asc" name="btnSubmit" />
<input type="submit" value="Desc" name="btnSubmit" />
<br />
@if (Model.teams.Count > 0)
{
for (int i = 0; i < Model.teams.Count; i++)
{
@Html.EditorFor(m => m.teams[i].TeamName)
@Html.HiddenFor(m => m.teams[i].TeamID)
@Ajax.ActionLink("Update team", "_PartialTeams", "Home", new {model = this.Model, id = Model.teams[i].TeamID}, null);
<input type="button" value="Remove team" name="btnSubmit" />
<br />
}
}
</div>
当我单击该链接时,出现以下错误:
如果我将其从ajax链接更改为标准html链接,则会出现运行时错误屏幕,因为它是同步的
这是我的文件结构:
我希望它是一个调用正确方法的Ajax链接,这样我就可以异步更新该行。从服务器错误图像中,您可以看到MVC希望解析部分文件的ActionLink引用的位置: ~/Views/Shared/\u PartialTeams.chshtml或~/Views/Home/\u PartialTeams.chshtml 在文件夹结构的屏幕截图中,_PartialTeams.chshtml文件位于Partials文件夹下 将文件移动到/Views/Home/文件夹或/Views/Shared/下,您的解决方案就会工作 根据评论 在控制器中,部分团队操作使用
return PartialView("~/Views/Partials/_PartialTeams.cshtml", modelTeamSelect);
但是有一个警告,一旦你开始脱离惯例,其他人就很难开始使用你的代码,而且随着时间的推移,它会降低可维护性你能给我你的模型结构modelTeamSelect吗?@PragneshKhalas是的,我将更新我的帖子。你的控制器代码在哪里?没有办法解析URL使其指向部分?我想保持我的文件结构井然有序。@nickgowdy在你的控制器操作结果方法中使用return~/Views/Partials/\u PartialTeams.cshtml,但有一点需要注意,一旦你开始脱离惯例,其他人就很难开始使用你的代码,随着时间的推移,这会降低可维护性我将partialview移到了共享文件夹,我意识到我没有很好地描述我的问题。我想使用actionlink更新团队。因此,视图和局部视图应使用新值再次加载。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Website.Models
{
public class modelTeamSelect
{
public modelTeamSelect()
{
teams = new List<modelTeam>();
team = new modelTeam();
}
public List<modelTeam> teams { get; set; }
public modelTeam team { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Website.Models
{
public class modelTeam
{
public Guid TeamID { get; set; }
public string TeamName { get; set; }
public Guid? LeagueID { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Website.Models;
using Website.Repository;
namespace Website.Controllers
{
public class HomeController : Controller
{
private FootballRepository FootballRepository;
public ActionResult TeamManagement()
{
modelTeamSelect modelTeamSelect;
FootballRepository = new FootballRepository();
modelTeamSelect = FootballRepository.GetTeams();
return View(modelTeamSelect);
}
[HttpPost]
public ActionResult TeamManagement(string btnSubmit, modelTeamSelect modelTeamSelect)
{
switch (btnSubmit)
{
case "Add Team":
// For now - add to collection but not use DAL
modelTeamSelect.teams.Add(modelTeamSelect.team);
//modelTeamSelect.team.TeamName = string.Empty;
break;
}
return View(modelTeamSelect);
}
#region PartialView
public PartialViewResult _PartialTeams(modelTeamSelect modelTeamSelect)
{
return PartialView("_PartialTeams", modelTeamSelect);
}
[HttpPost]
public PartialViewResult _PartialTeams(string BtnSubmit, modelTeamSelect modelTeamSelect)
{
FootballRepository = new Repository.FootballRepository();
switch (BtnSubmit)
{
case "Add Team":
FootballRepository.AddTeam(modelTeamSelect.team);
modelTeamSelect.teams.Add(modelTeamSelect.team);
break;
case "Asc":
modelTeamSelect.teams = FootballRepository.Sort(modelTeamSelect, BtnSubmit);
ModelState.Clear();
break;
case "Desc":
modelTeamSelect.teams = FootballRepository.Sort(modelTeamSelect, BtnSubmit);
ModelState.Clear();
break;
}
return PartialView("~/Views/Partials/_PartialTeams.cshtml", modelTeamSelect);
}
[HttpPost]
public ActionResult _PartialTeams(modelTeamSelect modelTeamSelect)
{
return PartialView("~/Views/Partials/_PartialTeams.cshtml", modelTeamSelect);
}
#endregion
public ActionResult MatchSelect()
{
ViewBag.Message = "Your app description page.";
return View();
}
}
}
return PartialView("~/Views/Partials/_PartialTeams.cshtml", modelTeamSelect);