Domanda

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?

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top