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 4116930
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:46:17+00:00 2026-05-20T22:46:17+00:00

I am a bit lost here as I have not really looked at model

  • 0

I am a bit lost here as I have not really looked at model binders so if possible, can one advise me if I am actually thinking about my problem correctly… 🙂 and if my code is way of, please advise…

1 -I have a DTO class which contains ‘custom fields’ each with a name and other properties i.e.:

Public Class CustomFields
{
 public string Name {get;set;}
 public string Description {get;set;}
 public string FieldType {get;set;}
 public string Value {get;set;}
}

2- Within my repo/business layer I am setting the values and returning ICollection for the view to render

3- the view uses a foreach to display fields

<% foreach (var item in Model) {%>
   <div class="editor-label">
      <%= Html.Label(item.Name) %>
   </div>
   <div class="editor-field">
      <%= Html.TextBox(item.Name, item.Value)%>
   </div>
<% } %>

Question: What is the best way to retrieve the result via a post? If there are errors I need to send back the errors to the view…

NOTE: What I did–>

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index([ModelBinder(typeof(CustomModelBinder))] ICollection<CustomFields> Fields)
{
//code here...
}

Custom Model binder gets values from form collection and transforms in into the correct type- is this correct? The best way to do this? I get the feeling I overcomplicated things…

public class CustomModelBinder : IModelBinder
{
 public object BindModel(ControllerContext controllerContext, 
 ModelBindingContext    bindingContext)
 {

  var fields = new List<CustomFields>();

  var formCollection = new FormCollection(controllerContext.HttpContext.Request.Form);

  foreach (string _key in formCollection)
  {
    if (_key.Contains("_RequestVerificationToken"))
         break;

    fields.Add(new CustomFields { Name = _key, 
    Value = formCollection.GetValue(_key).AttemptedValue });
  }
  return fields;
 }
}
  • 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-05-20T22:46:18+00:00Added an answer on May 20, 2026 at 10:46 pm

    Everything is perfect until step 3 where you mention foreach loops in a view. That’s where I would stop and use editor templates instead. So replace the loop in your view by:

    <%= Html.EditorForModel() %>
    

    and inside the corresponding editor template which will be rendered for each element of the model collection (~/Views/Shared/EditorTemplates/CustomFields.ascx):

    <div class="editor-label">
       <%= Html.LabelFor(x => x.Name) %>
    </div>
    <div class="editor-field">
       <%= Html.TextBoxFor(x => x.Name) %>
    </div>
    <div class="editor-field">
       <%= Html.TextBoxFor(x => x.Value) %>
    </div>
    <div class="editor-field">
       <%= Html.TextBoxFor(x => x.Description) %>
    </div>
    <div class="editor-field">
       <%= Html.TextBoxFor(x => x.FieldType) %>
    </div>
    

    then simply:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Index(IEnumerable<CustomFields> fields)
    {
        //code here...
    }
    

    No need of any model binders. The editor template will take care of generating proper names for the input fields so that they are correctly bound.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a bit lost here, solution could be lurking under my nose but
Bit of an obscure one this. My setup is all running on my local
any bit of info will be helpful here. or does anybody else huge do
A bit new with WPF...It seems like not matter what I change with horizontal
This is a bit of a long shot, but if anyone can figure it
I just wonder a bit whether or not GDI+ is still a technology worth
I am a bit lost, this looks like some silly mistake - but I
I have a Datatable that contains a column called Tags, Tags can have values
I have a question because I'm really bad at SQL. I understand basic functions
I've tried searching around for answers to this but I'm still a bit lost.

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.