Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,

I want to do a javascript request on the save method, like this:

C#
[AuthorizeLogin]
       public ActionResult UpdateSave(UpdateAgendaEventViewModel viewModel)
       {

           ReservationModel reservation = null;
           if (ModelState.IsValid)
           {
               ReservationModel r;
               Mapper.CreateMap<UpdateAgendaEventViewModel, ReservationModel>()
                   .ForMember(d => d.ReservationId, opt => opt.MapFrom(s => s.id))
                   .ForMember(d => d.FuneralId, opt => opt.MapFrom(s => s.funeralId))
                   .ForMember(d => d.RoomId, opt => opt.MapFrom(s => s.roomId))
                   .ForMember(d => d.Begin, opt => opt.MapFrom(s => s.start))
                   .ForMember(d => d.End, opt => opt.MapFrom(s => s.end))
                   .ForMember(d => d.RoomItems, opt => opt.MapFrom(s => s.RoomItems));
               if (!string.IsNullOrEmpty(viewModel.id))
               {
                   r = new ReservationModel(int.Parse(viewModel.id));
                   r.Update(Mapper.Map<UpdateAgendaEventViewModel, ReservationModel>(viewModel));
               }
               else
               {
                   r = Mapper.Map<UpdateAgendaEventViewModel, ReservationModel>(viewModel);
               }

               Mapper.CreateMap<UpdateAgendaEventViewModel, ReservationModel>();
               ReservationModel newInfo = Mapper.Map<UpdateAgendaEventViewModel, ReservationModel>(viewModel);
               reservation.Update(Mapper.Map<UpdateAgendaEventViewModel, ReservationModel>(viewModel));

               reservation.Save();




           }

           return Json(new { succes = true });
       }


and the java javascript:
JavaScript
$(function () {

    $.ajax({

        url: '/AgendaEvent/UpdateSave/',
        type: 'POST',                                    
        data: jsonData,
        datatype: 'html',
       // data: JSON.stringify(dataResponse),
        //contentType: 'application/json; charset=utf-8',
        //dataType: 'json',
        error: function (response) {
            alert("Error");
        },
        success: function (data) { console.log(data) }
    });
});


But I get this error:

Uncaught ReferenceError: jsonData is not defined


data: jsonData,

Thank you

Oke,

I have it now like this:

XML
// [AuthorizeLogin]
    public ActionResult UpdateSave(UpdateAgendaEventViewModel viewModel)
    {

        ReservationModel reservation = null;
        if (ModelState.IsValid)
        {
            ReservationModel r;
            Mapper.CreateMap<UpdateAgendaEventViewModel, ReservationModel>()
                .ForMember(d => d.ReservationId, opt => opt.MapFrom(s => s.id))
                .ForMember(d => d.FuneralId, opt => opt.MapFrom(s => s.funeralId))
                .ForMember(d => d.RoomId, opt => opt.MapFrom(s => s.roomId))
                .ForMember(d => d.Begin, opt => opt.MapFrom(s => s.start))
                .ForMember(d => d.End, opt => opt.MapFrom(s => s.end))
                .ForMember(d => d.RoomItems, opt => opt.MapFrom(s => s.RoomItems));
            if (!string.IsNullOrEmpty(viewModel.id))
            {
                r = new ReservationModel(int.Parse(viewModel.id));
                r.Update(Mapper.Map<UpdateAgendaEventViewModel, ReservationModel>(viewModel));
            }
            else
            {
                r = Mapper.Map<UpdateAgendaEventViewModel, ReservationModel>(viewModel);
            }

           // Mapper.CreateMap<UpdateAgendaEventViewModel, ReservationModel>();
           // ReservationModel newInfo = Mapper.Map<UpdateAgendaEventViewModel, ReservationModel>(viewModel);
            //reservation.Update(Mapper.Map<UpdateAgendaEventViewModel, ReservationModel>(viewModel));

           // r.Save();

            RoomModel room = new RoomModel(r.RoomId.Value);
            bool boolEditable = (!r.Definitive.HasValue) ? true : false;


            if (r.Save())
            {


                return Json(new AgendaEventModel()
                {
                    className = boolEditable ? "" : "agenda-event inactive",
                    id = r.ReservationId.ToString(),
                    title = room.Name,
                    start = string.Format("{0:yyyy-MM-ddTHH:mm:ss}", r.Begin),
                    end = string.Format("{0:yyyy-MM-ddTHH:mm:ss}", r.End),
                    color = room.Color,
                    backgroundColor = room.BackgroundColor,
                    borderColor = room.BorderColor,
                    textColor = room.TextColor,

                    editable = boolEditable,
                    eventStartEditable = boolEditable,
                    eventDurationEditable = boolEditable,
                });
            }

            else
            {
                //this.ShowMessage(Message.Type.Danger, Resources.Default.SaveFailure, true);
                //this.ShowMessage(Message.Type.Danger, r.Error, true);
                this.ShowMessage(Message.Type.Danger, r.Error);
            }
        }

        return Json(new { succes = true });
    }



and the ajax call:
JavaScript
$(function () {
                               //var premisesViewModel = $('form').serializeObject();
                               $.ajax({

                                   url: '/AgendaEvent/UpdateSave/',
                                   type: 'POST',
                                   data: {},
                                   datatype: "JSON",
                                  // data: JSON.stringify(dataResponse),
                                   //contentType: 'application/json; charset=utf-8',
                                   //dataType: 'json',
                                   error: function (response) {
                                       alert("Error");
                                   },
                                   success: function (data) { console.log(data) }
                               });
                           });


But all the values of the model are null
Posted
Updated 13-Nov-14 21:28pm
v3
Comments
Richard Deeming 13-Nov-14 10:40am    
You don't have a variable called jsonData defined in your script.
[no name] 13-Nov-14 10:42am    
thank you, but how to do that?
Richard MacCutchan 13-Nov-14 11:03am    
This is not Java it's Javascript; two completely different things. And why did you tag your C# code as XML?
[no name] 13-Nov-14 11:08am    
? I had javascript
Richard MacCutchan 13-Nov-14 11:26am    
You tagged the question "Java", and as you can see from my strikeout, you also had java instead of javascript in your question.

1 solution

Why dont you use
HTML
@using.BeginForm("UpdateSave","AgendaEvent")
{
//Form goes here..
}
This
would send the form data as in the viewmodel. You should try and avoid ajax call when you can use the Html helper in MVC & Razor(the view engine).
Else if you call this function from the onclick of the Submit button, then I would suggest anticipating that you have a form which you want to submit and update.
So use an id for the form and try the ajax as
JavaScript
$.ajax({

       url: '/AgendaEvent/UpdateSave/',
       type: 'POST',
       data: $("#FORMID").serialize(),
       success: function (data) {
       console.log(data)
                },
       error: function (response) {
           alert("Error");
       }
   });

This serialize method serializes all the form element data and sends the data to the server.
Please try like this , else post your Html code for the form.
This is just my anticipation.
Hope this works out.
thanks.
:)
 
Share this answer
 
Comments
[no name] 14-Nov-14 3:19am    
Thank for answare, but it has to goes through javascript, the calendar.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900