Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing JSON object to Controller but loose model binding
    primarykey
    data
    text
    <p>I am trying to pass a simple JSON object to a controller using MVC3 and JSON. The object gets passed but I loose all the properties. I see all the properties in firebug on the request but am not sure why I am loosing them on the server. Do all the properties of the object need to be set in order for the mapping to work? I am using MVC3 so the binding should be build in. What am I missing?</p> <p>Class:</p> <pre><code>[Serializable] public class StoryNote { public int Id { get; set; } public string Note { get; set; } public Nullable&lt;int&gt; StoryCardId { get; set; } public string CreatedBy { get; set; } public Nullable&lt;System.DateTime&gt; CreateDate { get; set; } public virtual StoryCard StoryCard { get; set; } } </code></pre> <p>JSON:</p> <pre><code>$(document).ready(function () { $('#newNote').click(function (e) { e.preventDefault(); var storynote = { StoryNote: { Note: $('#Note').val(), StoryCardId: $('#StoryCard_Id').val(), CreatedBy: 'Xyz', } }; $.ajax({ url: '@Url.Action("PostNote")', type: 'POST', data: JSON.stringify(storynote), dataType: 'json', contentType: 'application/json; charset=utf-8', success: function (data) { $('#previousNotes').append(data.Note + '&lt;br/&gt;' + data.CreatedBy + '&lt;br/&gt;&lt;hr/&gt;'); }, }); }); }); </code></pre> <p>Controller:</p> <pre><code> [HttpPost] public JsonResult PostNote(StoryNote newStoryNote) { StoryNote newNote = new StoryNote { Note = newStoryNote.Note, CreatedBy = newStoryNote.CreatedBy, StoryCardId = newStoryNote.StoryCardId, CreateDate = DateTime.Now }; db.StoryNotes.Add(newStoryNote); return Json(newStoryNote, JsonRequestBehavior.AllowGet); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload