Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8934015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:41:21+00:00 2026-06-15T09:41:21+00:00

I am passing to json objects to an asp.net mvc controller action. Both parameters

  • 0

I am passing to json objects to an asp.net mvc controller action. Both parameters are null.

Can someone spot the error prolly wrong naming?

/* Source Unit */
var sourceParent = sourceNode.getParent();
var sourceUnitParentId = sourceParent == null ? null : sourceParent.data.key;
var sourceUnit = { unitId: sourceNode.data.key, parentId: sourceUnitParentId };
var sourceUnitJson = JSON.stringify(sourceUnit);

/* Target Unit */
var targetParent = targetNode.getParent();
var targetUnitParentId = targetParent == null ? null : targetParent.data.key;
var targetUnit = { unitId: targetNode.data.key, parentId: targetUnitParentId };
var targetUnitJson = JSON.stringify(targetUnit);

moveUnit(sourceUnitJson, targetUnitJson);



function moveUnit(sourceUnit, targetUnit) {
        $.ajax({
            url: '@Url.Action("Move", "Unit")',
            type: 'POST',
            data: { sourceUnit: sourceUnit, targetUnit: targetUnit },
            success: function (response) {

            },
            error: function (e) {

            }
        });
    }

[HttpPost]
        public ActionResult Move(DragDropUnitViewModel sourceUnit, DragDropUnitViewModel targetUnit)
        {
            Unit sUnit = Mapper.Map<DragDropUnitViewModel, Unit>(sourceUnit);
            Unit tUnit = Mapper.Map<DragDropUnitViewModel, Unit>(targetUnit);
            _unitService.MoveUnit(sUnit, tUnit);
            return new EmptyResult();
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:41:22+00:00Added an answer on June 15, 2026 at 9:41 am

    Why don’t you use a view model? If you want to pass JSON to a controller action then define a view model containing the 2 properties and then JSON.stringify the entire request.

    Here’s your view model:

    public class MoveViewModel
    {
        public DragDropUnitViewModel SourceUnit { get; set; }
        public DragDropUnitViewModel TargetUnit { get; set; }
    }
    

    Now your controller action will take the view model as argument:

    [HttpPost]
    public ActionResult Move(MoveViewModel model)
    {
        Unit sUnit = Mapper.Map<DragDropUnitViewModel, Unit>(model.SourceUnit);
        Unit tUnit = Mapper.Map<DragDropUnitViewModel, Unit>(model.TargetUnit);
        _unitService.MoveUnit(sUnit, tUnit);
        return new EmptyResult();
    }
    

    and finally invoke this controller action using AJAX and send JSON request by making sure that you have specified the correct request content type, otherwise ASP.NET MVC wouldn’t know how to deserialize back the JSON request:

    /* Source Unit */
    var sourceParent = sourceNode.getParent();
    var sourceUnitParentId = sourceParent == null ? null : sourceParent.data.key;
    var sourceUnit = { unitId: sourceNode.data.key, parentId: sourceUnitParentId };
    
    
    /* Target Unit */
    var targetParent = targetNode.getParent();
    var targetUnitParentId = targetParent == null ? null : targetParent.data.key;
    var targetUnit = { unitId: targetNode.data.key, parentId: targetUnitParentId };
    
    /* build the view model */
    var moveModel = { sourceUnit: sourceUnit, targetUnit: targetUnit };
    
    /* Pass the view model to the server using an AJAX request */
    moveUnit(moveModel);
    
    
    
    function moveUnit(moveModel) {
        $.ajax({
            url: '@Url.Action("Move", "Unit")',
            type: 'POST',
            // It's very important to specify the correct content type
            // request header because we are sending a JSON request
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify(moveModel),
            success: function (response) {
    
            },
            error: function (e) {
    
            }
        });
    }
    

    Summary:

    • Every time you have a controller action taking more than 1 single argument you are doing it wrong. Stop and define a view model immediately.
    • Every time you have a controller action passing a domain model to a view you are doing it wrong. Stop and define a view model immediately.

    As you can see it’s all about view models in ASP.NET MVC.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using return Json(whatever); inside an ASP.NET MVC Action to return data to my
I'm passing to single JSON type parameter and multiple JSON type parameters to Asp.Net.
I have a ASP.NET web service decorated with System.Web.Script.Services.ScriptService() so it can return json
I am implementing an error handling strategy in an ASP.Net MVC 3 application. I've
I'm passing JSON parameter to asp.net C#. and the parameter could be single or
I am trying to use the JQuery plugin jqGrid with an asp .net mvc
I have an asp.net MVC 2.0 application. I also have a WFC service, if
I am using ASP.Net Web API with JSON.Net to serialize. I had to configure
I am using ASP.MVC 1 to return an IEnumerable of objects (say, Cars): public
I'm attempting to parse JSON response to convert ASP.NET dates to javascript dates. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.