Well I have a backbone.js model here
define ['jquery','underscore','backbone'] , ($,_,Backbone) ->
class Student extends Backbone.Model
idAttribute : "UserKey"
urlRoot : "Api/Student"
defaults:
UserKey: null
FirstName : ""
LastName : ""
Username : ""
Password : ""
Age : 18
initialize : ->
#console.log "You have been initialized"
And Lets see I have a controller here : -
public class HomeController : Controller
{
private Repository<Student, PortalContext> repo;
public HomeController()
{
repo = new Repository<Student, PortalContext>(new PortalContext());
}
[HttpGet]
public ActionResult All()
{
IList<Student> studs = repo.GetAll().ToList<Student>();
return this.Json(new { Students=studs }, JsonRequestBehavior.AllowGet);
}
[HttpGet]
public JsonResult Get(int id)
{
Student student = repo.FindBy(x => x.UserKey ==id).Select(x=>x).SingleOrDefault<Student>();
return this.Json(student,JsonRequestBehavior.AllowGet);
}
[HttpPut, ActionName("Student")]
public ActionResult Update(Student student)
{
repo.Edit(student);
repo.Save();
return Json(student);
}
}
And here is my View.Coffee file where I am saving the Model :
define ['jquery'
,'underscore'
,'backbone'
,'text!/Templates/Student/View.htm'] , ($ , _ , Backbone , ViewTemplate) ->
class StdView extends Backbone.View
_.templateSettings = { interpolate: /\{\{(.+?)\}\}/g };
template : _.template(ViewTemplate)
events : ->
"click #back":"Back"
"click #save" : "Save"
Save : ->
console.log "tryng to save"
@model.save
'FirstName' : @$el.find("#txtFirstName").val()
'LastName' : @$el.find("#txtLastName").val()
'Age': @$el.find("#txtAge").val()
,
wait:true
success: =>
alert "Saved"
@
Back: (e)->
window.history.back()
false
initialize : (options) ->
@render()
render :->
#console.log "this is details"
@setElement @template @model.toJSON()
@
It sometime saves the model but sometimes it posts back to the server i.e the backbone doesnt save the data rather it attaches the data into browser url like this
and posts back . But sometimes it saves the data and shows the alert . I am not sure why this is happening Can someone put some light why it is happening like that? ?