JavaScript Creating a ViewModel in MVC from Javascript for a Post method create viewmodel from model,create viewmodel from entity framework,create

I am trying to create a Partial View that is a submission form for creating a a new ProductionGoal model. It uses ProductionLineViewModel to create that.

My main question is how to pass that data into my CreateNewProductionGoal controller method. I wrote some cursory JS but I am still new to JS and am not totally sure what I am doing. I used this link as a basis to write my JS: How to post data from ViewModel into a controller method?

Currently when I press the button the CreateNewProductionGoal method does not get called. I am wondering if I need to add something to get that to happen or if I have other errors.

<input id="submit" type="button" class="button" value="Submit" onclick="onClick();">

function onClick() {
    alert("I am an alert box!");

    var Employees = $("#productiongoal-text").data("kendoNumericTextBox").value();
    var ProdLineId = $("#productionLine-dropdown").data("kendoDropDownList").value();
    var ProductionGoalViewModel = { "NumberOfEmployees": Employees, "ProductionLineId": ProdLineId };
    var requestData = {}
    var data = { request: requestData, pgvm: ProductionGoalViewModel }

    $.ajax({
        type: 'post',
        url: "ProductionLine/CreateNewProductionGoal",
        dataType: "json",
        data: data,  //Creating a ProductionGoalViewModel to pass into the CreateNewProductionGoal method
        success: function (data) {
            location = location;  //Refreshes the page on button press
        }
    });

}

[HttpPost]
    public ActionResult CreateNewProductionGoal([DataSourceRequest] DataSourceRequest request, ProductionGoalViewModel pgvm)
    {
        if (pgvm != null && ModelState.IsValid)
        {
            ProductionGoal pg = new ProductionGoal();
            pg.NumberOfEmployees = pgvm.NumberOfEmployees;
            pg.NumberOfUnits = _prodLineService.Find(pgvm.ProductionLineId).UPE * pgvm.NumberOfEmployees;
            pg.ProductionLineId = pgvm.ProductionLineId;
            pg.ProdLine = _prodLineService.Find(pgvm.ProductionLineId);
            pgvm.NumberOfUnits = pg.NumberOfUnits;
            pgvm.Id = pg.Id;
            pgvm.CreatedAt = pg.CreatedAt;
            _prodGoalService.Insert(pg);
        }
        return Json(new[] { pgvm }.ToDataSourceResult(request, ModelState));
    }

I want the button press to pass a view model that has NumberOfEmployees and ProductionLineId to the CreateNewProductionGoal method.

I can try to clarify more if needed.

EDIT:

var ProductionGoalViewModel = { "NumberOfEmployees": Employees, "ProductionLineId": ProdLineId };
var data = { pgvm: ProductionGoalViewModel }

data: data,

EDIT 2:

I am now certain that it has something to do with my button not calling the onClick() method. I put an alert in that method, probably should have done this awhile ago, and that alert is never being shown. Any suggestions?

<input id="submit" type="button" class="button" value="Submit">

function onClick() {
    var Employees = $("#productiongoal-text").data("kendoNumericTextBox").value();
    var ProdLineId = $("#productionLine-dropdown").data("kendoDropDownList").value();
    var ProductionGoalViewModel = { "NumberOfEmployees": Employees, "ProductionLineId": ProdLineId };
    var data = { pgvm: ProductionGoalViewModel }

    alert("I am an alert box!");

    $.ajax({
        type: 'post',
        url: "ProductionLine/CreateNewProductionGoal",
        dataType: "json",
        data: data,  //Creating a ProductionGoalViewModel to pass into the CreateNewProductionGoal method
        success: function (data) {
            location = location;  //Refreshes the page on button press
        }
    });

}

EDIT 3: I figured out why my button was never able to call the JS functions. My button was defined in a partial view and the view that the partial was being called in did not contain the script that was being called. Pretty frustrating but I am glad I am actually able to call something with my button now. However, I am still not able to call the CreateNewProductionGoal method. I have updated the original code to match what I currently have.

Answer:1

Your code all seems correct to me. When using your JS make sure the tags "#___" match correctly. A lot of times this can be a headache to realize.

Also as Hafiz mentioned above you need to pass two parameters.

var requestdata ={ };
var pgvmdata = { "NumberOfEmployees": Employees,
                "ProductionLineId": ProdLineId };

var data = {request:requestdata ,pgvm:pgvmdata}
$.ajax({
        url: "/ProductionLine/CreateNewProductionGoal",
        type: 'post',
        dataType: "json",
        data: data,
        success: function (data) {
            location = location;  //Refreshes the page on button press
        }
    });
Answer:2

You are facing this issue just because you have to pass two parameters and you try to pass only one. I don't know what exactly you have to pass for request so it's up to you so for time being it's empty.

Ajax request looks like.

var requestdata ={ };

var pgvmdata = { "NumberOfEmployees": Employees,
                    "ProductionLineId": ProdLineId };

    var data = {request:requestdata ,pgvm:pgvmdata}
    $.ajax({
            url: "/ProductionLine/CreateNewProductionGoal",
            type: 'post',
            dataType: "json",
            data: data,
            success: function (data) {
                location = location;  //Refreshes the page on button press
            }
        });
Answer:3

Try changing,

       data: JSON.stringify({ 
       //Creating a ProductionGoalViewModel to pass into the CreateNewProductionGoal method
            "ProductionGoalViewModel": {
                "NumberOfEmployees": Employees,
                "ProductionLineId": ProdLineId
            }
        }),

To this

       data: {
           "NumberOfEmployees": Employees,
           "ProductionLineId": ProdLineId
       },
Answer:4

My task: I have a file that contains many items and each item is related to an array of URLs of images which I need to download. I want to download all of the links, I'm using this library for the ...

My task: I have a file that contains many items and each item is related to an array of URLs of images which I need to download. I want to download all of the links, I'm using this library for the ...

  1. wait async function javascript
  2. wait async function js
  3. wait async function
  4. wait async function c#
  5. wait async function typescript
  6. wait for async function to finish javascript
  7. wait for async function to finish
  8. wait for async function to finish c#
  9. wait for async function to complete javascript
  10. wait for async function node js
  11. nodejs wait async function
  12. node wait async function
  13. wait for async function flutter
  14. wait for async function swift
  15. wait for async function angular
  16. wait for async function to finish js
  17. wait for async function to finish typescript
  18. wait for async function react
  19. wait for async function to finish swift
  20. wait for async function java

How the variable 'str2' is available inside the callback method passed to display method? str2 should be visible only inside function 'name'. a = { display: function (n){ console.log("I ...

How the variable 'str2' is available inside the callback method passed to display method? str2 should be visible only inside function 'name'. a = { display: function (n){ console.log("I ...

Whenever I hover over #Rock div I want it to apply a style to both .RockToScissors & .RockToLizard at the same. A the moment I have: <div onmouseover="overRock()" onmouseout="outRock()"> ...

Whenever I hover over #Rock div I want it to apply a style to both .RockToScissors & .RockToLizard at the same. A the moment I have: <div onmouseover="overRock()" onmouseout="outRock()"> ...

  1. apply multiple style using jquery
  2. apply multiple style in javascript
  3. apply style to multiple classes
  4. css apply style multiple classes
  5. apply style to multiple layers qgis
  6. apply style to multiple ids
  7. apply style to multiple elements
  8. apply style to multiple tags
  9. apply style to multiple layers
  10. apply css style to multiple elements
  11. pandas style apply multiple
  12. apply css style to multiple ids
  13. apply css style to multiple selectors
  14. apply same style to multiple classes
  15. darktable apply style to multiple images
  16. sass apply style to multiple classes
  17. scss apply style to multiple classes
  18. javascript apply style to multiple elements
  19. jquery apply style to multiple elements
  20. less apply style to multiple classes

Hi i've tried to display an image on jsp but failing miserably. Im generating a chart and saving a the file than refreshing it, now i want to show this picture called chart on jsp. i know this is all ...

Hi i've tried to display an image on jsp but failing miserably. Im generating a chart and saving a the file than refreshing it, now i want to show this picture called chart on jsp. i know this is all ...

  1. amazon having trouble displaying orders
  2. we're having trouble displaying this page
  3. directv now having trouble displaying this video
  4. we're having trouble displaying this book
  5. we're having trouble displaying this video directv now
  6. we're having trouble displaying your passes
  7. we are having trouble displaying this page
  8. we are having trouble displaying this video
  9. we're having trouble displaying this page nokia lumia
  10. we're having trouble displaying all newsfeed updates