我的控制器中有两个不同名称的操作但是当我尝试将数据发送到第二个时,我收到一条错误说:
控制器类型的当前动作请求'索引' 'HouseholdController'在以下操作之间不明确 方法:System.Web.Mvc.ActionResult在类型上查找(Int32) WhatWorks.Controllers.HouseholdController System.Web.Mvc.ActionResult 关于类型WhatWorks.Controllers.HouseholdController
的索引(Int32)
我发现的所有其他问题都与具有相同名称的动作有关。我无法解决我出错的地方,尽管可能有更好的方法来完成我正在尝试的...
控制器代码
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;
}
}
答案 0 :(得分:2)
尝试在您的查找方法中添加[AcceptVerbs(HttpVerbs.Post)]
或[HttpPost]
属性:
[HttpParamAction]
[HttpPost]
public ActionResult Find(int Id)
{
var model = GetDisplay().TakeWhile(m => m.familyId == Id);
return View("Index", model);
}
答案 1 :(得分:1)
我修改了BeginForm以显式引用Find动作,这样做没有任何额外的属性。
我想我只是期待一个更复杂的解决方案!
@using (Html.BeginForm("Find", "Household"))