mcintyre321 / FormFactory

MVC5, Core or standalone - Generate rich HTML5 forms from your ViewModels, or build them programatically

Home Page:http://formfactoryaspmvc.azurewebsites.net/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Get Values from Dynamically Created PropertyVm[]

opened this issue · comments

Posting to the action results in a null PropertyVM[]

I want to get the saved values from the server generated PropertyVM[].

I'm I missing something here?

public ActionResult Index()
       {
           var formModel = new[]
                   {
               new PropertyVm(typeof(string), "username")
               {
                   DisplayName = "Username",
                   NotOptional = true,
               },
               new PropertyVm(typeof(string), "password")
               {
                   DisplayName = "Password",
                   NotOptional = true,
                   GetCustomAttributes = () => new object[] {new PasswordAttribute()}
               },
               new PropertyVm(typeof(string), "os")
               {
                   DisplayName = "Operating System",
                   NotOptional = true,
                   Choices = new List<string>() {"OSX", "IOS", "Windows", "Android"},
                   Value = "Windows", //Preselect windows
                   GetCustomAttributes = () => new object[] {new RadioAttribute()},
               }
           };

           return View("Index", formModel);

       }

Index cshtml

<p>...will render this form:</p>
        @model PropertyVm[]
        @using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
                @Model.Render(Html);

                <input type="submit" value="Submit" />
        }

Action result for post

        [HttpPost]
        public ActionResult Index(PropertyVm[] vm)
        {
            var results = vm; //this is always null
            return RedirectToAction("Index");
        }

So the short answer is this: you are going to have to post a JToken, then save that dynamic bag of data into your system.

public ActionResult Index(JToken vm) { ...save into db... }

The longer answer is: if you are using PropertyVms, it means you are using data driven fields, (rather than using FF against static viewmodel types), and you are POSTing data that represents values for those fields, so you need to take the POSTed data, and validate it against the field definitions from your data.

If this doesn't make sense, please let me know a little about what you are doing exactly and I can explain with example.

I am trying to retrieve the values for the data driven fields, like you are saying.
If you happen to have an end to end example of data driven fields (view and controller) for setting and getting data from a small form, that would be great!

This is probably my unfamiliarity of MVC core. Still running into a few issues, with the post.

     public ActionResult Index(Newtonsoft.Json.Linq.JToken vm)
        {
            var results = vm; //this is always null
            return RedirectToAction("Index");
        }

When I post given this, I end up with.
InvalidOperationException: Could not create an instance of type 'Newtonsoft.Json.Linq.JToken'. Model bound complex types must not be abstract or value types and must have a parameterless constructor.

Not that familiar with core either - try JArray, JObject, FormCollection or even dynamic- if that doesn't work then we will need to find a custom ModelBinder which binds to a dynamic collection.

OK, so the answer is to bind to IFormCollection and then use this extension method to convert it to a nested JObject https://gist.github.com/mcintyre321/f565869c452a3ffd00bfdad2f14f093f

Let me know how you get on!

This works perfectly! Thank you so much.