I'm trying to add a custom list of parameters (guids) to the controller. For testing purpose I got a simple class:
public class MyVM
{
public string Name { get; set; }
public Guid Id { get; set; }
public List<MySubVM> MySubVms { get; set; }
}
public class MySubVM
{
public string Name { get; set; }
}
And I show it on my page like this
@using (Html.BeginForm("DoPost", "Home", FormMethod.Post, new { id = "form", enctype = "multipart/form-data" }))
{
@Html.TextAreaFor(m => m.Name)
for (int i = 0; i < Model.MySubVms.Count; i++)
{
@Html.TextBoxFor(m => Model.MySubVms[i].Name)
}
<button class="btn form-control btn-success" type="submit" name="buttonType" value="SaveEvaluation">Save</button>
}
My controller method looks like this
[HttpPost]
public ActionResult DoPost(MyVM vm, List<string> guids)
{
return View();
}
The Jquery
<script type="text/javascript">
var guids = [{ id: "6650f183-14fa-4247-9639-612c4ca0a232" }, { id: "97aa91a3-28a4-44f3-8d96-f93d5eaef558" }];
$.ready(function() {
$('form').on('submit', function(event) {
event.preventDefault();
});
$.ready(function() {
$('form').on('submit', function(event) {
event.preventDefault();
$.post({
type: 'POST',
url: "/Home/DoPost",
dataType: "json",
data: { vm: $("form").serialize(), guids: guids },
success: function() {
alert("Successful");
}
});
});
});
});
The issue is that the model gets posted correct into the controller, but the list of guids doesn't work. It's null all the time. I tried with string and it's the same problem there.