这可能是一个非常基本的问题,但由于某些原因,我的链接无法找到控制器和方法。
我在部分视图中有一个名为_PartialTeams.cshtml的Ajax.ActionLink
@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>
这是在此视图中引用的部分视图
@model Website.Models.modelTeamSelect
@{
ViewBag.Title = "Football App";
}
@section featured {
}
@using (Ajax.BeginForm("_PartialTeams",
new
{
model = this.Model
},
new AjaxOptions
{
HttpMethod = "POST",
UpdateTargetId = "divCreatedTeams",
InsertionMode = InsertionMode.Replace
}))
{
<div id="divTeams" style="float: left; padding: 10px;">
<h3>Create a new team:</h3>
@Html.LabelFor(m => m.team.TeamName)
@Html.TextBoxFor(m => m.team.TeamName)
<input type="submit" value="Add Team" name="btnSubmit" />
</div>
Html.RenderPartial("~/Views/Partials/_PartialTeams.cshtml");
}
Website.Models.ModelTeamSelect
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();
}
}
}
当我点击链接时出现此错误:
如果我将它从ajax链接更改为标准html链接,我会得到运行时错误屏幕,假设因为它是同步的
这是我的文件结构:
我希望它是一个调用正确方法的Ajax链接,以便我可以异步更新该行。
答案 0 :(得分:0)
从服务器错误图像中,您可以看到MVC正在寻找解析部分文件的ActionLink参考的位置:
“〜/ Views / Shared / _PartialTeams.chshtml”或“〜/ Views / Home / _PartialTeams.chshtml”
在文件夹结构的屏幕截图中,_PartialTeams.chshtml文件位于Partials文件夹下。
移动/ Views / Home /文件夹下的文件或/ Views / Shared /,您的解决方案将起作用。
根据评论
在您的控制器_PartialTeams操作中使用
return PartialView("~/Views/Partials/_PartialTeams.cshtml", modelTeamSelect);
但是一旦你开始摆脱惯例就会有一个警告,其他人开始使用你的代码会更加困难,而且随着时间的推移会降低可维护性