C# MVC中的操作请求不明确,其中的操作具有不同的名称
我的控制器中有两个名称不同的操作,但当我尝试将数据发送到第二个时,会出现一个错误,显示: 控制器类型上操作“索引”的当前请求 “HouseholdController”在以下操作之间不明确 方法:System.Web.Mvc.ActionResult在类型上查找(Int32) WhatWorks.Controllers.HouseholdController System.Web.Mvc.ActionResult WhatWorks.Controllers.HouseholdController类型上的索引(Int32) 我发现关于这个问题的所有其他问题都与同名动作有关。我不知道我哪里出了问题,尽管可能有更好的方法来完成我正在尝试的事情 控制器代码C# MVC中的操作请求不明确,其中的操作具有不同的名称,c#,asp.net-mvc,partial-views,C#,Asp.net Mvc,Partial Views,我的控制器中有两个名称不同的操作,但当我尝试将数据发送到第二个时,会出现一个错误,显示: 控制器类型上操作“索引”的当前请求 “HouseholdController”在以下操作之间不明确 方法:System.Web.Mvc.ActionResult在类型上查找(Int32) WhatWorks.Controllers.HouseholdController System.Web.Mvc.ActionResult WhatWorks.Controllers.HouseholdController
public ActionResult Index(int page = 1)
{
int pagesize = 10;
var model = GetDisplay().OrderBy(i => i.familyId);
return View(model.ToPagedList(page, pagesize));
}
//
// GET: /HouseholdSearch/
public ActionResult Search()
{
return PartialView("Find");
}
[HttpParamAction]
public ActionResult Find(int Id)
{
var model = GetDisplay().TakeWhile(m => m.familyId == Id);
return View("Index", model);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Reflection;
public class HttpParamActionAttribute : ActionNameSelectorAttribute
{
public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
{
if (actionName.Equals(methodInfo.Name, StringComparison.InvariantCultureIgnoreCase))
return true;
var request = controllerContext.RequestContext.HttpContext.Request;
return request[methodInfo.Name] != null;
}
}
局部视图“查找”
尝试将
[AcceptVerbs(HttpVerbs.Post)]
或[HttpPost]
属性添加到查找方法:
[HttpParamAction]
[HttpPost]
public ActionResult Find(int Id)
{
var model = GetDisplay().TakeWhile(m => m.familyId == Id);
return View("Index", model);
}
我已经修改了BeginForm以显式引用Find操作,这在没有任何额外属性的情况下工作 我想我只是在期待一个更复杂的解决方案
@using (Html.BeginForm("Find", "Household"))
您的HttpParamAction属性代码是什么?我想这就是问题所在——我假设这是一个ActionNameSelectorAttribute,它的响应是“Index”是该操作的有效名称。您定义了任何自定义路由吗?@JacobM我已经添加了上面的代码。是@zimdanen,未定义自定义路线。p.s.w.g建议的[HTTPPost]属性在其命中正确操作的范围内工作,但当它应该转到索引视图时,它只是围绕查找操作循环。可能是因为按钮具有
name=“Find”
,这导致HttpParamActionAttribute中的行显示返回请求[methodInfo.name]!=null
返回true。也就是说,请求[“Find”]不是空的,因此Find方法报告它是匹配的。谢谢,这是有效的,但我得到了另一个可能与此相关的错误。如果需要的话,我会继续研究,并发布一个新问题。@melkisadek没问题。很高兴提供帮助:)新的错误是由于调用“索引”视图直接返回到查找操作。我不明白为什么这些动作有唯一的名称时会发生这种情况。
@using (Html.BeginForm("Find", "Household"))