0

I have an issue with aps.net mvc project. I have a view where is set list of check boxes inside of begin form, then on submit appropriate fields are shown as a report. So I have to add a button to my view, where user is setting which fields he d like to see, so as he could save all checkbox values as a preset with its name.

I have model with a lot of nested models it looks like this:

 public class EmployeeOverallReport
{
    public List<PersonalData> ListOfPersonalData { get; set; }
    public EmployeeOverallReportBool ColumnsNeeded { get; set; }
    public EmployeeOverallReportFilters ModelFilters { get; set; }
}

I actually need ColumnsNeeded model, which has alot of bool properties for storing each checkbox value (true/false).

So on click I have to get current state of checkboxes and make a post with these model values.

I have been trying to serialize my form:

var data = $('#myForm').serialize();
     $.post(url, { presetName: Name, entry: data}, function (data) {
         $("#saveResult").html("Saglabats");
     });

I got JSON string but it was invalid and i could not deserialize it back.

Here is what I am trying to do now:

    $("#savePresetButton").on('click', function () {
         var url = "/Reports/SavePreset";
         var data = @Html.Raw(Json.Encode(Model));
         $.post(url, { presetName: Name, entry: data}, function (data) {
             $("#saveResult").html("Saglabats");
         });
     });

this code is using my viewModel with its properties, where all ColumnsNeeded properies are set to false, as it is by default and all user side changes in a view are not set to model yet, so as my form was not submitted and values were not changed.

How could I get current state of all checkboxes on user side? Not doing it like :

 var dataset = {
            CategoryBool: $("#ColumnsNeeded_CategoryBool").val(),
            NameBool: $("#ColumnsNeeded_NameBool").val(),
            KnowledgeLevelBool: $("#ColumnsNeeded_KnowledgeLevelBool").val(),
            SkillAdditionalInfoBool: $("#ColumnsNeeded_SkillAdditionalInfoBool").val(),
            ...
           }

because I have more than 90 properties there..

I am sorry for this post, but posting some code is impossible due to count of properties inside the model.

GeekyNuns
  • 287
  • 2
  • 20
  • What is the controller method you posting to. If you serializing the model using `.serialize()` and you want to include additional properties, the you can use `.param()` to add the extra values - refer [this answer](http://stackoverflow.com/questions/32353093/mvc-jquery-ajax-post-returns-null/32353268#32353268) for an example –  Oct 21 '16 at 11:00

1 Answers1

1

Could you serialize the entire form and ajax submit the form itself?

see: Pass entire form as data in jQuery Ajax function

Community
  • 1
  • 1
ntimofeev
  • 51
  • 4
  • using serialize() returns incorrect JSON object which I cant use after. Code was fallowing `var data = $('#myForm').serialize(); $.post(url, { presetName: Name, entry: data}, function (data) { $("#saveResult").html("Saglabats"); });` – GeekyNuns Oct 21 '16 at 09:01
  • If it was me, I'd be changing the POST Controller's model to match this serialized data or creating an extra controller to deal with this data, rather than trying to map data from the form into a different JSON format. The whole point of MVC is to make this passing around and updating models easy – ntimofeev Oct 21 '16 at 09:25