我的控制器中有两个名称不同的操作,但是当我尝试将数据发送到第二个时,我收到一条错误消息:
当前对控制器类型“HouseholdController”的操作“Index”请求在以下操作方法之间不明确: System.Web.Mvc.ActionResult Find(Int32) on type WhatWorks.Controllers.HouseholdController System.Web.Mvc.ActionResult Index(Int32 ) 在 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 BootstrapSupport
@model WhatWorks.ViewModels.HouseholdListViewModel
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset class="form-horizontal">
@Html.LabelFor(model => model.familyId, new { @class = "control-label" })
<div class="controls">
@Html.TextBoxFor(model => model.familyId, new { @class = "input-mini" })
@Html.ValidationMessageFor(model => model.familyId, null, new { @class = "help-inline" })
</div>
<div>
<button type="submit" name="Find" class="btn ">Search</button>
</div>
</fieldset>
}
HTTPParamAction 代码
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;
}
}