简体   繁体   中英

Dictionary<,> Binding to a Controller in Asp.Net MVC

I have a class like as below.

   public class ProductViewGridModel
    {
        public long Id { get; set; }
        public string PackageCode { get; set; }
        public string ProductName { get; set; }
        public string ProductCategory { get; set; }
        public IDictionary<string, string> Localizations { get; set; }

    }

I rendered the model in to view like this.(foreach statement running into table, I'm not showing its here.)

 @foreach (var localization in Model.Localizations)
                {
                    var p = localization.Value;
                    <tr>
                        <td class="adminData">
                            <input class="k-textbox" type="text" value="@p" name="productView.Localizations[@localization.Key]" />
                            <input type="hidden" value="@p" name="productView.Localizations[@localization.Key]" />
                        </td>
                    </tr>
                }

And I have a button, the button sending form values to controller with ajax call.

Other model bindings are okay, but I want to bind all localization inputs to localizations model. But localizations always null. How can I bind this ?.

The input name is wrong. The default modelbinder will look for the property Localizations :

@foreach (var localization in Model.Localizations)
{
    var p = localization.Value;
    <tr>
        <td class="adminData">
            <input class="k-textbox" type="text" value="@p" name="Localizations[@localization.Key]" />
            <input type="hidden" value="@p" name="Localizations[@localization.Key]" />
        </td>
    </tr>
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM