title: backbone and mvc
body: <p>I have a Backbone project where I have the following model that I want to pass on to .NET for database updating.</p>
  
  <pre><code>var myList = Backbone.Model.extend({
  
      // Default attributes for the todo item.
      defaults: function () {
          return {
              Name: "Default name"
          };
      },
  
  
  
      url: "/my/myList",
  
      "sync": mySyncFunction,
  
      // Ensure that each todo created has `title`.
      initialize: function () {
          if (!this.get("Name")) {
              this.set({ "Name": this.defaults.Name });
          }
      }
  });
  </code></pre>
  
  <p>Using the following for overriding the sync</p>
  
  <pre><code>function mySyncFunction(method, model, options) {
      if (method == 'GET') {
  
          options.url = model.url;
      }
      else if (method == "create") {
  
          options.url = model.url+"Post";
      }
      else {
  
          options.url = model.url;
      }
  
      Backbone.emulateJSON = true
  
      return Backbone.sync(method, model, options);
  }
  </code></pre>
  
  <p>When creating a new item it is using the model.url+"Post" I assumed and the model.get("Name") contains the correct data. The correct section is executed and the entry in the database is created but the Name is empty.</p>
  
  <p>Although when the .NET Controller handles the post the Name no longer contains any data.</p>
  
  <p>My controller code looks like this.</p>
  
  <pre><code>   public class myController : Controller
      {
  
          public ActionResult myList()
          {
  
  
              List&lt;myCore.myList&gt; l = new List&lt;myCore.myList&gt;();
              l = myCore.myList.ListAll();
              return Json(l, JsonRequestBehavior.AllowGet);
  
          }
  
  
  
  
          [HttpPost]
          public ActionResult myListPost(myCore.myList doc)
          {
  
  
              doc.AccountGuid = Guid.NewGuid();
  
              Boolean created = doc.Create();
  
              return Json(doc);
          }
  
          [HttpPut]
          public ActionResult myListPut(myCore.myList doc)
          {
  
              myCore.myList doc1 = new myCore.myList();
              doc1.AccountGuid = Guid.Empty;
              doc1.Name = "testPut";
              Boolean created = doc1.Create();
  
              return Json(doc1);
          }
  
  
          [HttpDelete]
          public ActionResult myListDelete(myCore.myList doc)
          {
              //DeleteDoc(id);
              myCore.myList doc1 = new myCore.myList();
              doc1.id = Guid.NewGuid();
              doc1.AccountGuid = Guid.Empty;
              doc1.Name = "testDelete";
              Boolean created = doc1.Create();
  
              return Json(doc1);
          }
      }
  </code></pre>
  
  <p>Can anyone tell me what to do to get the model/class data into the controller.</p>
  
  <p>The myList class looks like this.</p>
  
  <pre><code>public class myList
          {
              Guid _id = Guid.Empty;
              String _name = String.Empty;
              Guid _accountGuid = Guid.Empty;
              public Guid id
              {
                  get { return _id; }
                  set { _id = value; }
              }
  
              public String Name
              {
                  get { return _name; }
                  set { _name = value; }
              }
  
              public Guid AccountGuid
              {
                  get { return _accountGuid; }
                  set { _accountGuid = value; }
              }
  }
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>It now looks like it works after removing Backbone.emulateJSON = true</p>
  
  <p>Although I still cant get my delete part to work. It comes up with a 404 error. Only Post and Get works.</p>
  
  <p>It looks like this in the controller.</p>
  
  <pre><code>[ActionName("myList")]
      [HttpDelete]
      public ActionResult myListDelete(myCore.myList doc)
      {
  
          doc.Name += "-DELETE";
  
  
          return Json(doc);
      }
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Figured that one out as well.</p>
  
  <p><a href="https://stackoverflow.com/questions/11155528/asp-net-handle-put-delete-verbs">ASP.NET Handle PUT/DELETE verbs</a></p>
  
  <p>Although i can seem to pass any model or paramters along with it on order to delete the correct entry in the database. </p>