0

I am currently developing a Framework to generate dynamic Views in MVC, the idea is based on this tutorial.

The next step is adding the possibility to generate multiple submit buttons but I cant get it to work. I did some research and found this approach. However, since i want to generate those buttons dynamically, this does not work yet.

What I tried is to modify this attribute code here:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class MultipleButtonAttribute : ActionNameSelectorAttribute
{
    public string Name { get; set; }

    public string Argument { get; set; }

    public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
    {
        var isValidName = false;
        var keyValue = string.Format("{0}:{1}", Name, Argument);

        var value = controllerContext.Controller.ValueProvider.GetValue(keyValue);

        if (value != null)
        {
            controllerContext.Controller.ControllerContext.RouteData.Values[Name] = Argument;
            isValidName = true;
        }

        return isValidName;
    }
}

While debugging I digged down the ValueProvider and found out that the JQueryFormValueProvider in the Collection of Valueproviders actually contains the Name of the clicked submitbutton.

The button is generated like this:

<input type="submit" name="buttonClick:@Model.Id" value="@Model.Text" />

Here is the Value i am looking for

Unfortunately the ValueProviders do not let me Iterate through the Keys so I dont know how to get the value I circled red in the above screenshot. It doesnt need to be this approach, all what counts is, to find out which button was clicked.

Community
  • 1
  • 1
CSharpie
  • 9,195
  • 4
  • 44
  • 71

1 Answers1

0

Ok i found the solution by modifying the Attribute-code like this:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class NameToRouteDataAttribute : ActionNameSelectorAttribute
{
    /// <Summary>Name of the Value you want to grab from the control.</Summary>
    public string Name { get; set; }

    public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
    {
        ValueProviderCollection providers = controllerContext.Controller.ValueProvider as ValueProviderCollection;

        if (providers != null)
        {
            // We need this value-Provider as it contains the Data from the Form
            JQueryFormValueProvider formProvider = providers.OfType<JQueryFormValueProvider>().FirstOrDefault();
            if (formProvider != null)
            {
                // now look for the specified value-prefix.
                var kvp = formProvider.GetKeysFromPrefix(Name).FirstOrDefault();
                if (kvp.Key != null)
                    controllerContext.Controller.ControllerContext.RouteData.Values[Name] = kvp.Key;
            }
        }
        return true;
    }
}

Contollercode:

    [HttpPost]
    [NameToRouteDataAttribute(Name = "buttonClick")]
    public ActionResult Show(FormViewModel form)
    {
        string buttonId = RouteData.GetRequiredString("buttonClick");

        ...
    }

Buttoncode

@model MvcForms.Controls.ButtonViewModel
<input type="submit" name="buttonClick.@Model.Id" value="@Model.Text" />

The important thing here is, that the name of the Button contains a dot. This makes the JQueryFormValueProvider.GetKeysFromPrefix work the way I need it to.

CSharpie
  • 9,195
  • 4
  • 44
  • 71