Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass dictionary from view to controller after form submit

Razor view:

@using (Html.BeginForm("Move", "Details", new { dict= dictionary}, FormMethod.Post)) {
    //table with info and submit button
    //dictionary => is a dictionary of type <Sales_Order_Collected, string>
}

Controller action:

[HttpPost]
public string Move(Dictionary<Sales_Order_Collected, string> dict) {
    return "";
}

Is there a way to pass the model dictionary to the controller? Because my parameter is always null.

like image 732
Sllix Avatar asked Feb 19 '26 21:02

Sllix


1 Answers

you can not pass dictionary through route values. You can do something like this:

@using (Html.BeginForm("Move", "Details", null, FormMethod.Post)) {
    <input type="text" name="[0].Key" value="first key"/>
    <input type="text" name="[0].Value" value="first value"/>

    <input type="text" name="[1].Key" value="second key"/>
    <input type="text" name="[1].Value" value="second value"/>
}

This will post Dictionary. The idea for complex objects is the same

like image 145
karaxuna Avatar answered Feb 21 '26 14:02

karaxuna