0
votes

I'm creating dynamic user input with knockout lib, BUT is when I submit the form the server side doesn't containts all the data I sent originally; i'm using a model that contains a IEnumerable property. The view specify the model is ReportModel, and also if I debug the JS i'm able to see the fieldValue change of all the fields correctly. I also use Html.BeginForm... to post the form, and IN MY ACTION ON THE CONTROLLER I JUST GET THE FIRST FIELD (ALWAYS THE FIRST), and if it helps, the value I got it's correct one (the one I set with the input)

my server side model:

public class ReportField
{
    public string fieldName {get; set;}
    public string fieldValue {get; set;}
    //other properties
}
public class ReportModel
{
    ReportFieldpublic List<ReportField> fields{ get; set; }
}

here is my client side code:

var viewModel = { reportModel: ko.mapping.fromJS(@Html.Raw(Json.Encode(Model)))};
$(document).ready(function () {
    ko.applyBindings(viewModel);
};

and the bind to the input:

//...foreach
<input type="text" data-bind="value: fieldValue, attr: { name: ' + fieldName() +'}" />

any help is welcome, thanks!

EDIT: when I bind the other properties of ReportField with hidden inputs, then on the server side I get all my fields and values correctly... why is that? how could I send all the fields to the server and avoid to use input hidden?

the controller action:

[HttpPost]
public ActionResult myAction(ReportModel model)
{
    foreach(ReportField field in model.fields){
        //model.fields only have one item when I not bind of the other properties on a hidden
    }
    //rest of the code
}

the form submit:

@using (Html.BeginForm("myAction", "Controller", FormMethod.Post, new { @id = "reportForm" }))

<input type="submit" value="OK" />

$(document).ready(function () {
     $("#reportForm").submit(function () {
                var valid = isValidForm();                    
                return valid;
            });
}

so far the solution is to bind some property of ReportField to a hidden input, then the action receive all the fields correctly... just wondering about is there another way.

2
Show us your controller action code.Jasen
Also, this answer might help.Jasen
i added the controller actionuser3170129
Can you show your submit code in your cshtml?ganders
You didn't add the actual submit, you just added the @using (Html.BeginForm(....)). What I'm asking is, show the line of code that looks like "<input type=submit.....>"ganders

2 Answers

0
votes

Make your "myAction" a post. Then the model will be passed.

0
votes

I would suggest you build your own model binder for this. If I understand this correctly, you are creating a form from the fields that you dynamically send down.

The problem is that when the params are passed back up they are passed individually. The MVC model binder won't be able to put it back together as a List because it doesn't know how to do that. The built in model binder will convert based on named arguments.

See this answer