1

I have an issue with the project I'm working on. I'm using Entity Framework. Some quick background on the db model:

public class AssetType{
    public ICollection<Field> Fields { get; set; }
}

public class Field{
    public int Id {get;set;
    public string Name {get;set;
}

Now I'm creating a view that would create a new Asset Type. As part of this process the user must also create all of the fields they want for that type. The issue is that I'm not sure how to represent the list of "Fields" on the page. The idea is that the user can add a new field, or remove one at any time with jQuery.

I can't figure how the data could be posted back to the server as part of the form. I thought about constructing the list in JSON form, but this seemed a bit messy. Has anyone got any better ideas?

1 Answer 1

2

You're going to have problems with this. The object parser does not handle complex objects very well. Collections usually need to be primitive types, or collections of primitive types themselves.

There are ways to do it, but if this is a requirement for you, I would look at storing your data in a JSON string variable, and parsing it where/ when needed.

Sign up to request clarification or add additional context in comments.

4 Comments

How about inserting hidden fields like: <input type="hidden" id="Fields[0].Id" value="5">
It really doesn't parse well, it's difficult to read and troubleshoot, but you may be able to get it to work... but at some point you'll need to ask yourself how much of the wheel you'll want to reinvent.
Thanks, I think I'll go with the JSON option. As you say, it'll be cleaner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.