1

The controller ...

[HttpPost]
public virtual ActionResult PickAColour(ColourModel model, 
                                        string imgbtn, string returnUrl) {

and the view ...

@using (Html.BeginForm(MVC.Home.PickAColour(Model,"",(string)ViewBag.ReturnUrl))) {
    @Html.LabelFor(m => m.SomeProperty)
    @Html.TextBoxFor(m => m.SomeProperty)
    ...
    ...
    <p>Now pick a colour</p>
    <input type="image" name="imgbtn" src="@Links.Content.Images.A_png" value="A"/>
    <input type="image" name="imgbtn" src="@Links.Content.Images.B_png" value="B"/>
    <input type="image" name="imgbtn" src="@Links.Content.Images.C_png" value="C"/>
    <input type="image" name="imgbtn" src="@Links.Content.Images.D_png" value="D"/>
    <input type="image" name="imgbtn" src="@Links.Content.Images.E_png" value="E"/>
    <input type="image" name="imgbtn" src="@Links.Content.Images.F_png" value="F"/>
} 

Now, this won't work because I didn't pass imgbtn parameter into the method. I don't know what is the correct way to do it?

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
Tom
  • 15,781
  • 14
  • 69
  • 111

1 Answers1

2

You must use the correct method overload:

@Html.BeginForm(MVC.Home.ActionNames.PickAColour, MVC.Home.Name,
                new { imgbtn = "", returnUrl = (string)ViewBag.ReturnUrl },
                FormMethod.Post, null)

As for the complex model object Model, you can't pass it using a route value like above. This one should be submitted when clicking a submit button so that ASP.NET MVC Model Binder can do its work.

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
  • I did a test using your way, it runs, but still string imgbtn=null when debug is entering the PickAColour method. Seems like the same result as my original method call? – Tom Jun 22 '12 at 12:09
  • Also, by the way, the Model object actually goes well either way, returnUrl is also good. Only imgbtn parameter doesn't seem to connect up. – Tom Jun 22 '12 at 12:18
  • Try changing the name of this parameter to make a test... Give an id="imgbtn" for the inputs and try again. – Leniel Maccaferri Jun 22 '12 at 12:20
  • Confirmed. image button type="image" is causing the problem. MVC can only regonise type="submit" as a button can parse its value. WTH?! didn't know this. Is there a way to get around it using my colourful buttons, instead of boring window gray buttons? – Tom Jun 22 '12 at 12:21
  • 1
    Here's a possibility I used in the past: http://stackoverflow.com/q/4896439/114029 – Leniel Maccaferri Jun 22 '12 at 12:30
  • Cool. Thanks, I will play around with the CSS :) the logic works now. – Tom Jun 22 '12 at 12:47