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);
}
}
}