Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8232025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:41:32+00:00 2026-06-07T17:41:32+00:00

I need to get my model properties in Post action, so need to hidden

  • 0

I need to get my model properties in Post action, so need to hidden element for them, but I have a problem with type of Dictionary <string, string>. this is My model:

public class ViewModel{
 ...
 public ViewPart ViewPart { get; set; }
}

public class ViewPart {
 ...
 public Dictionary<string, string> Flags { get; set; }
}

And My Controller:

Dictionary<string, string> flags = new Dictionary<string, string>();
flags.Add("kind", "Edit");
flags.Add("Command", "Save");
ViewModel model = new ViewModel(){ Flags  = flags };
return View(model);

In View:

@foreach(var item in Model.ViewPart.Flags) { 
 <input type="hidden" id="ViewPart_Flags_@(item.Key)" value="@item.Value" name="ViewPart.Flags[@(item.Key)]" />
}

Also I try This one :

@foreach(var item in Model.ViewPart.Flags) { 
  @Html.HiddenFor(modelItem => item)
}

Update
Post Action:

[HttpPost]
public ActionResult MyPostAction(ViewModel model){
  //model.ViewPart.Flags is null
}

But in Post action the Flags is Always null, why? where is my fault? Is there any better way to pass variables from View To Action?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T17:41:34+00:00Added an answer on June 7, 2026 at 5:41 pm

    You need two hidden fields one for the Key and one for the Value if you want to modelbind to a dictionary:

    var index = 0;
    foreach (var item in Model.ViewPart.Flags)
    {
    
        <input type="hidden" value="@item.Key" 
                             name="ViewPart.Flags[@(index)].Key"/>
        <input type="hidden" value="@item.Value" 
                             name="ViewPart.Flags[@(index)].Value"/>
    
        index++;
    }
        <input type="submit" value="Save"/>
    

    Note, you will be also need a running index to make the model binder happy.

    Or if you don’t want to have a running you can solve with an addtional hidden Index field:

    foreach (var item in Model.ViewPart.Flags)
    {
    
        <input type="hidden" value="@item.Key" 
                             name="ViewPart.Flags.Index"/>
        <input type="hidden" value="@item.Key" 
                             name="ViewPart.Flags[@(item.Key)].Key" />
        <input type="hidden" value="@item.Value" 
                             name="ViewPart.Flags[@(item.Key)].Value" />
    }
        <input type="submit" value="Save"/>
    }
    

    You can find a lots of info about modelbinding with collections in this two article:

    • ASP.NET Wire Format for Model Binding to Arrays, Lists, Collections, Dictionaries
    • Model Binding To A List
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have model: [Validator(typeof(RegisterValidator))] public class RegisterModel { public string Name { get; set;
I am writing my views.py, in which i need to get the model instance
I am trying to get Android USB host mode to work; but I need
I need get all items these have no categories int? categoryId = null; var
I have linq request. I need get item.Title in select. how do this? var
I have two tables with a weak relation. I need get a text value
Need to get an div with overflow:hidden; not to wrap its children. I need
I need to reference a model's self in a :has_many declaration. I have a
Is there any way to get at Java internal properties, such as sun.arch.data.model ,
Given the following Model, public class A { public string Name { get; set;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.