1

I apologize if asking some questions these days. But appreciate for all kind helps. Actually, I need to pass data from View to Controller using MVC3 Razor.

Here is the View Code:

Html.RenderAction("Checks", Model.GetRoleNames);

Here is the Controller Code:

    public ActionResult Checks(string[] getRoleNames)
    {
        // The parameter is NULL which should NOT be!
        //ViewBag.Name = name;
        //return View(values);
        return View();
    }

Here is the Partial View Code:

@model string[]
@{
for (int i = 0; i < Model.Length; i++)
{
<input id="@ViewBag.Name@(i)" name="@ViewBag.Name" type="checkbox" value="@(Model[i])"    />
<label for="@ViewBag.Name@(i)">@Model.[i]</label>
<br />
}

}

The Problem: Model.GetRoleNames has values inside the View as I tried tracing line by line. But no value is passed to the Controller Action as parameter! I don't know the possible reason.

Can anyone help me please?

user2394196
  • 341
  • 5
  • 18
  • Check the route maps http://stackoverflow.com/questions/2140208/how-to-set-a-default-route-to-an-area-in-mvc – stanlyF Jun 08 '13 at 09:53

1 Answers1

2

This method expects route parameters in a slightly different form:

Html.RenderAction("Checks", new {getRoleNames = Model.GetRoleNames});

Update. As to why parameters should be provided in such a form one might refer to the description from MSDN:

routeValues

An object that contains the parameters for a route. You can use routeValues to provide the parameters that are bound to the action method parameters. The routeValues parameter is merged with the original route values and overrides them.

The only thing to be added here is how exactly this object is bound to the input parameters. Default model binder parses input object and tries to find correspondence between object's fields and names of Action's input parameters. So in the case above Action had a single parameter named getRoleNames, and that is the name model binder was looking for. Obviously object Model.GetRoleNames does not contain such field/property, while new {getRoleNames = Model.GetRoleNames} does.

Andrei
  • 55,890
  • 9
  • 87
  • 108
  • That was awesome! You just gave me a valuable help! But Would you please explain your answer? Albeit, it works fine I am not fully understand about the issue. I explicitly mean; what is the second parameter about? What does it do that cause the job to be done? – user2394196 Jun 08 '13 at 10:08
  • @user2394196, I've updated the answer with a bit of explanation. Does it make everything any clear? – Andrei Jun 08 '13 at 11:09
  • The modifications you made just clears the concept for me. I did not know about 'Model Binder' job here. Hope I can help you some other time just as you did for me ;) Thanks... – user2394196 Jun 08 '13 at 14:27