Skip to content

Instantly share code, notes, and snippets.

@pdwetz
Created August 15, 2014 01:39
Show Gist options
  • Save pdwetz/cdcc65cd982bb0714c09 to your computer and use it in GitHub Desktop.
Save pdwetz/cdcc65cd982bb0714c09 to your computer and use it in GitHub Desktop.
Nancy Binding Issue
namespace Nancy.Demo.ModelBinding
{
using System.Linq;
using Database;
using Models;
using Nancy.ModelBinding;
public class EventsModule : NancyModule
{
public EventsModule()
: base("/events")
{
Get["/"] = x =>
{
var model = DB.Events.OrderBy(e => e.Time).ToArray();
return View["Events", model];
};
Post["/"] = x =>
{
Event model = this.Bind();
var model2 = this.Bind<Event>("Location"); // Blacklist location
DB.Events.Add(model);
DB.Events.Add(model2);
return this.Response.AsRedirect("/Events");
};
Get["/json"] = x =>
{
var model = DB.Events.OrderBy(e => e.Time).ToArray();
return View["PostJsonEvent", model];
};
Post["/json"] = x =>
{
Event model = this.Bind();
DB.Events.Add(model);
return this.Response.AsJson(model);
};
}
}
}
<viewdata model="Nancy.Demo.ModelBinding.Models.Event[]"/>
<html>
<head>
<title>JSON Post Test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript">
// http://css-tricks.com/snippets/jquery/serialize-form-to-json/
$.fn.serializeObject = function () {
var o = {};
var a = this.serializeArray();
$.each(a, function () {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
$(function () {
$("#serializedSubmit").click( function(e) {
var data = $("#mainForm").serializeObject();
$.ajax({
type: "POST",
url: "/events/json",
contentType: "application/json",
dataType: "json",
data: JSON.stringify(data),
success: function (event) {
$("#message").text("Serialized and stringified. JSON contentType. Event result: " + JSON.stringify(event));
}
});
});
$("#mainForm").submit(function (e) {
e.preventDefault();
var data = $("#mainForm").serializeObject();
$.ajax({
type: "POST",
url: "/events/json",
dataType: "json",
data: data,
success: function (event) {
$("#message").text("Serialized. Default content type. Event result: " + JSON.stringify(event));
}
});
});
});
</script>
</head>
<body>
<h1>JSON Post Event</h1>
<form id="mainForm" method="POST" action="/events/json">
<label for="Name">Title</label>
<input type="text" name="Title" />
<label for="Location">Location</label>
<input type="text" name="Location" />
<input type="checkbox" name="Venues" value="1">Venue 1</input>
<input type="checkbox" name="Venues" value="2">Venue 2</input>
<input type="checkbox" name="Venues" value="3">Venue 3</input>
<input type="checkbox" name="Venues" value="4">Venue 4</input>
<label for="EventDate">Time</label>
<input type="text" name="Time" value="${System.DateTime.Now}" />
<button type="submit">Form Submit</button>
<button id="serializedSubmit" type="button">JSON String Submit</button>
</form>
<div id="message"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment