0

I'm building an app that uses the following HTML/Javascript. The user enters 5 props and clicks 'Create.' The Javascript puts all of the props into a list and sends them to an MVC controller via ajax request.

function ajaxPost(arr) {
    // This function makes an ajax post to NewTricks 'Create' action. This is where the trick creation code should be;
    $.ajax({
        type: "POST",
        url: "/NewTricks/Create",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(arr),
        success: function (data) { alert(data); },
        failure: function (errMsg) { alert(errMsg); }
    });
}

function getProps() {
    var PropList = [];

    for (i = 0; i < 5; i++) {
        PropList.push($("#prop" + i).val());
    }

    ajaxPost(PropList);
}

$("#createButton").click(getProps);
<div id="PropForm">
    <div>
        <input type="text" placeholder="Enter Prop Here" id="prop0" />
    </div>
    <div>
        <input type="text" placeholder="Enter Prop Here" id="prop1" />
    </div>
    <div>
        <input type="text" placeholder="Enter Prop Here" id="prop2" />
    </div>
    <div>
        <input type="text" placeholder="Enter Prop Here" id="prop3" />
    </div>
    <div>
        <input type="text" placeholder="Enter Prop Here" id="prop4" />
    </div>

    <div>
        <button id="createButton" class="btn btn-primary btn-lg">Create!</button>
    </div>
</div>

This portion seems to work fine. The error is in the controller below and I just can't figure out how to correct it. pc.Create(prop) gives squigly lines under 'prop' and says "Cannot convert type 'CreateMagic.data.models.Prop' to 'System.Web.Mvc.FormCollection'" I've excluded the rest of the controller code and the 'catch' for lengths sake.

    public ActionResult Create(IList<Prop> UserProps)
    {
        try
        {
            // Start by adding the props in UserPropList to the USERS prop
            // collection. This collection will be used for 'auto-fill' option
            // when the user is creating the UserProps
            PropsController pc = new PropsController();
            foreach (Prop prop in UserProps)
            {
                prop.PropName.ToLower();
                pc.Create(prop);
            }
        }
    }
Rob Greenlee
  • 1,217
  • 2
  • 8
  • 10
  • Your ajax method is sending an array of simple values (not a `Prop`). The method parameter should be `string[] UserProps` or similar –  Jan 11 '16 at 22:39
  • I changed the method parameter to string[] UserProps. I edited the foreach loop to instantiate a new prop, assign the string as it's name and call pc.Create(NewProp). I'm still getting the same error on NewProp. – Rob Greenlee Jan 11 '16 at 22:53
  • Is your array (the method parameter) being populated correctly in the POST method? If so can you edit your question to show the new code. The error message your getting (relating to `FormCollection`) makes no sense based on the code you have shown –  Jan 11 '16 at 22:58
  • I finally realized that the Create method in PropsController wanted a FormCollection as the parameter. Between that, your initial comment, and what johnnyRose answered, my problem has been solved! Thank you. – Rob Greenlee Jan 15 '16 at 20:15

1 Answers1

0

In getProps(), you are building a list of strings from your input tags. When you send that array through Ajax back to the server, the server has no idea how to convert strings to an IList<Prop>, which explains your error.

As Stephen Muecke mentioned in his comment, your controller action has to accept a string[] UserProps, and then you can use that in your PropsController.Create method.

Additionally, strings are immutable in C#, so calling prop.ToLower() only returns the lowercase string; it does not actually modify the name. Based on your example, you could just assign it to another variable (like this: string lowerCaseProp = prop.ToLower();), or just use the value directly.

Here's an example:

public ActionResult Create(string[] UserProps)
{
    PropsController pc = new PropsController();
    foreach (string prop in UserProps)
    {
        pc.Create(new Prop(prop.ToLower())); // or whatever your constructor is
    }
}

More info on string immutability here and here.

Community
  • 1
  • 1
johnnyRose
  • 7,310
  • 17
  • 40
  • 61