多个提交按钮在MVC3 - 的FormCollection有输入型提交任何键/值 - 远程验证问题? [英] Multiple Submit Buttons in MVC3 - FormCollection has no Key/Value for Input Type Submit - Remote Validation Issue?
问题描述
我在MVC3应用程序中的两个按钮。
<输入类型=提交名称=命令值=交易/> &安培; NBSP;
<输入类型=提交名称=命令值=所有交易/>
当我点击一个按钮,这回发正确,但的FormCollection没有命令键。我在模型中还加了属性命令,当窗体发布其值为null。
公众的ActionResult指数(的FormCollection的FormCollection,SearchReportsModel searchReportsModel)。 {
如果(searchReportsModel.command ==所有交易)
...
其他
....
}
我使用IE8。我如何使用MVC3多个按钮?是否有针对此问题的解决方法吗?我做了很多研究,但没有找到一个解决方案。结果
更新:
戴夫:我想你的解决方案,它抛出HTTP 404错误资源无法找到
下面是我的code:
[HttpPost]
[AcceptSubmitType(NAME =命令,类型=交易)]
公众的ActionResult指数(SearchReportsModel searchReportsModel)
{
返回RedirectToAction(交易,报告,新{...});
}[HttpPost]
[ActionName(指数)]
[AcceptSubmitType(NAME =命令,类型=所有交易)]
公众的ActionResult Index_All(SearchReportsModel searchReportsModel)
{
返回RedirectToAction(AllTransactions,报告,新{...});
}公共类AcceptSubmitTypeAttribute:ActionMethodSelectorAttribute
{
公共字符串名称{;组; }
公共字符串类型{搞定;组; } 公众覆盖布尔IsValidForRequest(ControllerContext controllerContext,MethodInfo的MethodInfo的)
{
返回controllerContext.RequestContext.HttpContext
.Request.Form [this.Name] == this.Type;
}
}
问题是在视图模型(SearchReportsModel)评论下面远程验证属性之后解决。它看起来像它在MVC3了一个错误:
// [远程(CheckStudentNumber,SearchReports的ErrorMessage =没有记录该学生数存在)]
公众诠释? StudentNumber {搞定;组; }
您也许可以用 ActionMethodSelectorAttribute
的属性并覆盖IsValidForRequest方法。你可以看到下面这个方法只是确定特定参数(名称)是否符合它的一个特性(类型)。它应该看起来像这样的视图模型绑定:
公共类TestViewModel
{
公共字符串命令{搞定;组; }
公共字符串moreProperties {搞定;组; }
}
属性看起来是这样的:
公共类AcceptSubmitTypeAttribute:ActionMethodSelectorAttribute
{
公共字符串名称{;组; }
公共字符串类型{搞定;组; } 公众覆盖布尔IsValidForRequest(ControllerContext controllerContext,MethodInfo的MethodInfo的)
{
返回controllerContext.RequestContext.HttpContext
.Request.Form [this.Name] == this.Type;
}
}
然后,您可以标记像这样的 AcceptSubmitType
属性,你的行动:
[AcceptSubmitType(NAME =命令,类型=交易)]
公众的ActionResult指数(TestViewModel VM)
{
//使用视图模型做什么
}//为伪重写指数行动
[ActionName(指数)]
[AcceptSubmitType(NAME =命令,类型=所有交易)]
公众的ActionResult Index_All(TestViewModel VM)
{
//使用视图模型做什么
}
这也消除了单个控制器的动作需要逻辑,因为它似乎是你真正需要行动的两个独立的课程。
I have the two buttons in MVC3 application.
<input type="submit" name="command" value="Transactions" />
<input type="submit" name="command" value="All Transactions" />
When I click on a button, it posts back correctly but the FormCollection has no "command" keys. I also added a property "command" in the model and its value is null when the form is posted.
public ActionResult Index(FormCollection formCollection, SearchReportsModel searchReportsModel). {
if (searchReportsModel.command == "All Transactions")
...
else
....
}
I am using IE8. How can I use multiple buttons in MVC3? Is there a workaround for this issue? I did lot of research and could not find a solution.
Update:
Dave: I tried your solution and it is throwing Http 404 error "The resource cannot be found".
Here is my code:
[HttpPost]
[AcceptSubmitType(Name = "Command", Type = "Transactions")]
public ActionResult Index(SearchReportsModel searchReportsModel)
{
return RedirectToAction("Transactions", "Reports", new { ...});
}
[HttpPost]
[ActionName("Index")]
[AcceptSubmitType(Name = "Command", Type = "All Transactions")]
public ActionResult Index_All(SearchReportsModel searchReportsModel)
{
return RedirectToAction("AllTransactions", "Reports", new { ... });
}
public class AcceptSubmitTypeAttribute : ActionMethodSelectorAttribute
{
public string Name { get; set; }
public string Type { get; set; }
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
{
return controllerContext.RequestContext.HttpContext
.Request.Form[this.Name] == this.Type;
}
}
The issue was resolved after commenting the following Remote validation attribute in the ViewModel (SearchReportsModel). It looks like it is a bug in MVC3:
//[Remote("CheckStudentNumber", "SearchReports", ErrorMessage = "No records exist for this Student Number")]
public int? StudentNumber { get; set; }
You might be able to get away with an ActionMethodSelectorAttribute
attribute and override the IsValidForRequest method. You can see below this method just determines whether a particular parameter (Name) matches one of it's properties (Type). It should bind with a view model that looks like this:
public class TestViewModel
{
public string command { get; set; }
public string moreProperties { get; set; }
}
The attribute could look like this:
public class AcceptSubmitTypeAttribute : ActionMethodSelectorAttribute
{
public string Name { get; set; }
public string Type { get; set; }
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
{
return controllerContext.RequestContext.HttpContext
.Request.Form[this.Name] == this.Type;
}
}
Then, you could tag your actions with the AcceptSubmitType
attribute like this:
[AcceptSubmitType(Name="command", Type="Transactions")]
public ActionResult Index(TestViewModel vm)
{
// use view model to do whatever
}
// to pseudo-override the "Index" action
[ActionName("Index")]
[AcceptSubmitType(Name="command", Type="All Transactions")]
public ActionResult Index_All(TestViewModel vm)
{
// use view model to do whatever
}
This also eliminates the need for logic in a single controller action since it seems you genuinely need two separate courses of action.
这篇关于多个提交按钮在MVC3 - 的FormCollection有输入型提交任何键/值 - 远程验证问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!