The issue persists with the MVC Controller parameter returning as null consistently, especially when the object field is

When using JavaScript to call a controller and process a function, I encountered an issue where the parameter value (name in summary) is null when the field is set to private. However, the function works correctly when the field is set to public. Is setting it to public the only solution? Or is there a better way to handle this situation? Thank you in advance.

My object

[DataContract]
public class Summary
{
 [DataMember]
 public int id { private set; get; }
 [DataMember]
 public string name { private set; get; }

 public summary() {}

 public summary(int id, string name)
 {
   id = id;
   name = name;
 }

}

MVC Controller

public ActionResult SetSummary(Summary summary)
 {
    string anme = summary.name; **<-- null if private**
    ...
 }

Javascript

$http.post("MyController/SetSummary", JSON.stringify({
            summary: mySummaryObject}))
        .success(function (data, status, headers, config) {
    ....
    }

Answer №1

Indeed, making it public is essential because the DefaultModelBinder effectively translates a user request into a data object. This particular class serves as a tangible implementation of a model binder.

In essence, the DefaultModelBinder class links various types of objects to a user's request:

Basic types like String, Double, Decimal, or DateTime.

Complex models such as Person, Address, or Product.

Collections like ICollection, IList, or IDictionary.

Source: https://msdn.microsoft.com/en-us/library/system.web.mvc.defaultmodelbinder(v=vs.118).aspx

I trust you found this information beneficial; please share any thoughts or feedback.

Thank you

Karthik

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Encountering a JavaScript Issue While Displaying an iFrame

Having trouble displaying an iframe on a ColdFusion website when passing values in the URL. A similar method worked fine on a non-ColdFusion site, so I suspect the issue is with ColdFusion itself. Unfortunately, I have no experience with ColdFusion. If I ...

Vue JS - Unable to locate module (datepicker)

Today marks my first time delving into the world of vue.js, and as expected, I've encountered an error that has me stumped. I recently incorporated the v-md-date-range-picker module into my project: (. The setup instructions provided me with the f ...

Utilize Next.js and GSAP to dynamically showcase images upon hovering over the title

I have a dynamic list of titles that I want to enhance by displaying images when hovering over each title. The issue I'm facing is that when I hover over one title, all the images display at once. As a React beginner, I believe the solution should be ...

Observable in RxJS with a dynamic interval

Trying to figure out how to dynamically change the interval of an observable that is supposed to perform an action every X seconds has been quite challenging. It seems that Observables cannot be redefined once they are set, so simply trying to redefine the ...

The problem of a circular reference arose when attempting to pass the model using the JSON.stringify method

I am looking to replace all fields with null values to prevent the model from proceeding further. However, when I attempt to create a replacer function and pass the model, I am encountering a circular reference issue. Is there a more effective way to accom ...

Angular restricts the use of the svg namespace in the $sce service for security reasons

I have a project in Angular Material where I am using an md-icon with a specific svg structure: <md-icon class="ic1" md-svg-src='data:image/svg+xml, <svg xmlns="https://www.w3.org/2000/svg" viewBox="0 0 32 32"> <ellipse ry="16" rx=" ...

What could be the issue with my injection supplier?

When running the following code, the configuration works fine, but an error is returned: Uncaught Error: [$injector:unpr] Unknown provider: AngularyticsConsoleHandlerProvider <- AngularyticsConsoleHandler <- Angularytics Code snippet: angular.modu ...

How to effectively delete the class from a navigation list item

Looking for some inspiration? Check out the basic visuals for this question here. But let me break it down for you. This snippet shows the HTML & CSS behind a tabbed-carousel, with a condensed version for clarity: <style> #myCarousel-100 . ...

New data was successfully appended via Ajax, however, the links seem to be malfunction

I have a code snippet that looks like this: $('.new-post').on('click', function(e) { e.preventDefault(); $.ajax({ url: '/somehting' data: {my data}, ...etc, success: function(data) { $.get('temp ...

Check for my variable in the redux state before proceeding

Currently, I am creating connection and registration screens, with a profile button on the bottom tab bar. The objective is for the user to be directed to their profile page if they are logged in (user data stored in Redux), or to a landing screen with log ...

Is it possible to prompt npm to install a sub-dependency from a GitHub pull request?

Currently, I'm in the process of setting up geofirestore, which has a dependency on geofirestore-core. However, there is an existing bug in geofirestore-core that has been addressed in a pull request. How can I make sure that my geofirestore installa ...

How to Retrieve a Targeted Element from a Multidimensional Array

I've got some data that looks like this: [[12, 23],[27,-6],[52, -32],[82, 11]] How can I access specific elements within these arrays? With a standard array like this: [a, b, c, d] It's easy to reference 'b' as arrayName[1]. But ho ...

How can I prevent 401 unauthorized errors from appearing in the console?

I am facing an issue with a Vue.js function that sends data to the back-end for user login. When the user doesn't exist, it returns a 401 unauthorized error. While I can handle this error, it keeps getting logged in the console. Is there a way to prev ...

Is there a way to adjust the font size in Javascript/Html without changing the color?

I have a code snippet that creates a button to increment a variable, and I want to change the font size of the displayed variable. This code is for a game akin to cookie clicker. <div class="game-object"> <script type="text/javascript>"; var c ...

Vue component fails to register

In my current project, I am incorporating a custom component tag using Vue.JS. Although we have successfully utilized Vue.JS in past projects, the same approach isn't working this time. It seems like I must have overlooked something... After inspect ...

Concern regarding the duration setting in the date-picker

Is there a specific "campaign duration" rule where you must select a date between the first and last day of the month? If you choose a date outside of this range, such as a date from the next month, the Backend API will return an "Input param error." Curr ...

Detecting empty or default form fields in Angular using Reactive Forms to disable buttons

Is it possible to disable the submit button when the initial form is empty, and enable it if any value is not empty? <form [formGroup]="registerForm" (ngSubmit)="onSubmit()"> <div class="form-row"> ...

Unexpected behavior encountered when running Angular 8 radio button checked function

I have an Angular 8 web app with some unique logic implemented as shown below: HTML: <div *ngFor="let item of selectedItems;"> <input type="radio" [(ngModel)]="mySelectedItem" [value]="item.key" (ngModelChange)="setCh ...

Transforming a Java calendar date into a JavaScript date by utilizing the getTimezoneOffset() method of the new Date object

I've been working with a calendar data that is sent to the server, which includes the following fields: export interface CalendarDate{ dayOfMonth: number; hourOfDay: number; minute: number; month: number; second: number; year: ...

Dynamic content cannot have classes added to them using jQuery

When adding content dynamically, it is necessary to use an event handler for a parent element using on(). However, I am facing an issue where the class added with addClass on dynamically generated content disappears immediately. Let's take a look at ...