I have a model class like this:
namespace Models
{
public struct Localization
{
public int Id;
public string LanguageName;
public string LanguageCode;
public DateTime LastUpdate;
}
public class LocalizationModel
{
private List<Localization> _localization;
public LocalizationModel() {
Browse();
}
//create simple list
public void Browse() {
Localization lang = new Localization();
_localization = new List<Localization>();
for ( int i = 1; i <= 3; i++ ) {
lang.Id = i;
lang.LanguageName = "Language name " + i;
lang.LanguageCode = "Language code " + i;
lang.LastUpdate = DateTime.Now;
_localization.Add( lang );
}
}
public bool AddNewLanguage( Localization item ) {
if ( ( item as object ) == null ) {
throw new ArgumentException( "The localization cannot be empty" );
}
_localization.Add( item );
return true;
}
public List<Localization> LocalizationList {
get {
return _localization;
}
}
}
}
And the Add method defined in Home Controller:
[HttpPost]
public ActionResult Add( Localization sendInfo ) {
bool success = _localization.AddNewLanguage( sendInfo );
return this.Json( new {
msg = success
} );
}
I called that method from jquery ajax function like this:
function sendDataForAdd() {
var sendInfo = { Id: 0,
LanguageName: $("#lang-name").val(),
LanguageCode: $("#lang-code").val(),
LastUpdate: $("#lang-update").val()
};
$.post('/Home/Add', sendInfo, doneLanguageAdded, 'json');
function doneLanguageAdded(msg) {
if (msg) {
alert("The language was added successfully !");
window.location.refresh(true);
}
else {
alert("The language was not added in list !");
}
}
}
The problem is that the Add function parameter -> sendInfo from controller becomes null.
The javascript variables take correctly the input values.
The problem is the struct from Model or what ?
I tried other scenario. I defined a class with all properties and use it in LocalizationModel class. It works perfectly with jquery ajax and pass correct parameter to controller function.
Why if I use struct, the controller parameter is null ? There are a trick ?
I put a breakpoint to controller function (using Visual Studio) and I checked the parameter.
Id and other variables are null. I attached a picture:

Thank you
Your struct needs to have getters and setters.