Multiple Submit Buttons in MVC3 - FormCollection has no Key/Value for Input Type Submit - Remote Validation Issue?

前端 未结 5 567
不思量自难忘°
不思量自难忘° 2021-01-25 22:19

I have the two buttons in MVC3 application.

      
   

        
相关标签:
5条回答
  • 2021-01-25 23:04

    @CodeRush: The W3C standard does allow more than 1 submit per form. http://www.w3.org/TR/html4/interact/forms.html. "A form may contain more than one submit button".

    0 讨论(0)
  • 2021-01-25 23:06

    This is a nice Blog about this found here

    I like the look of adding in AcceptParameterAttribute

    0 讨论(0)
  • 2021-01-25 23:10

    Correct me If I'm wrong, but according to W3C standard you should have only 1 submit button per form. Also having two controls with identical names is a bad idea.

    0 讨论(0)
  • 2021-01-25 23:10

    when you submit (on any button) your whole page is posted back to the controller action, I have had the same problem but have not found a decent solution yet.. maybe you could work with a javascript 'onclick' method and set a hidden value to 1 for the first button and 0 for the second button or something like that?

    0 讨论(0)
  • 2021-01-25 23:13

    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.

    0 讨论(0)
提交回复
热议问题