I am not sure my approach is correct so I am open to any suggestions.
I am using MVC 3. I am using the standard Membership provider and have modified the standard MVC Account model. I have a view that Registers the user details as well as requires them to add Apartment details. Apartment details are added using a jquery dialog
and saving/appending the results back to a table in the view. One user can register many apartments. Because I need to parse the table data in the view ( as well as the user details), I am using jQuery to parse the table data into JSON and then using a AJAX
Post to send the data back to the controller. This part is working correctly, the data is saved to the database. However, after a successfull Registration, I would like to redirect to a View that informs the user to check their email account for the account
confirmation. I know this redirect doesnt work in the Controller ( not sure why) but I cant get the redirect to work in the Ajax Post either: In fact , I cant even get teh success alert to fire. Can anyone tell me where I am going wrong?
$('#register').click(function(event) {
var apartmentRowValues =
$('#complexes-table tbody tr').map(function() {
// $(this) is used more than once; cache it for performance.
var $row = $(this);
// For each row that's "mapped", return an object that
// describes the first and second <td> in the row.
return {
ComplexName: $row.find(':nth-child(1)').text(),
ApartmentNo: $row.find(':nth-child(2)').text()
};
}).get();
var json = {
UserName: $('#UserName').val(),
Email: $('#Email').val(),
Password: $('#Password').val(),
ConfirmPassword: $('#ConfirmPassword').val(),
FirstName: $('#UserName').val(),
LastName: $('#LastName').val(),
Apartments: apartmentRowValues
};
$.ajax({
url: '/Account/Register/',
type: 'POST',
dataType: 'JSON',
data: JSON.stringify(json),
contentType: 'application/json; charset=utf-8',
success: function() { alert("Ajax Post succeeded"); },
error: function() { alert("An error has occurred in the ajax post of the registration data: " + err.Message); }
});
});
(Please select 'Mark as Answer' if my response has helped you.)
Yes, that is why I have posted my code. The data is saved to the database but neither the success or failure events fire. Can you see where there is an error?
(Please select 'Mark as Answer' if my response has helped you.)
I have checked the page source as well as the Jquery file - I removed 'err' and now the failure alert is displaying. Not sure why chrome isnt reporting an error
(Please select 'Mark as Answer' if my response has helped you.)
I have checked the page source as well as the Jquery file - I removed 'err' and now the failure alert is displaying. Not sure why chrome isnt reporting an error
calibur
Member
300 Points
124 Posts
I cant get a redirect to work
Mar 05, 2012 02:32 PM|LINK
I am not sure my approach is correct so I am open to any suggestions.
I am using MVC 3. I am using the standard Membership provider and have modified the standard MVC Account model. I have a view that Registers the user details as well as requires them to add Apartment details. Apartment details are added using a jquery dialog and saving/appending the results back to a table in the view. One user can register many apartments. Because I need to parse the table data in the view ( as well as the user details), I am using jQuery to parse the table data into JSON and then using a AJAX Post to send the data back to the controller. This part is working correctly, the data is saved to the database. However, after a successfull Registration, I would like to redirect to a View that informs the user to check their email account for the account confirmation. I know this redirect doesnt work in the Controller ( not sure why) but I cant get the redirect to work in the Ajax Post either: In fact , I cant even get teh success alert to fire. Can anyone tell me where I am going wrong?
$('#register').click(function(event) { var apartmentRowValues = $('#complexes-table tbody tr').map(function() { // $(this) is used more than once; cache it for performance. var $row = $(this); // For each row that's "mapped", return an object that // describes the first and second <td> in the row. return { ComplexName: $row.find(':nth-child(1)').text(), ApartmentNo: $row.find(':nth-child(2)').text() }; }).get(); var json = { UserName: $('#UserName').val(), Email: $('#Email').val(), Password: $('#Password').val(), ConfirmPassword: $('#ConfirmPassword').val(), FirstName: $('#UserName').val(), LastName: $('#LastName').val(), Apartments: apartmentRowValues }; $.ajax({ url: '/Account/Register/', type: 'POST', dataType: 'JSON', data: JSON.stringify(json), contentType: 'application/json; charset=utf-8', success: function() { alert("Ajax Post succeeded"); }, error: function() { alert("An error has occurred in the ajax post of the registration data: " + err.Message); } }); });ignatandrei
All-Star
137682 Points
22147 Posts
Moderator
MVP
Re: I cant get a redirect to work
Mar 05, 2012 02:52 PM|LINK
If the error does not fire also, you have a javascript error somewhere!
calibur
Member
300 Points
124 Posts
Re: I cant get a redirect to work
Mar 05, 2012 02:58 PM|LINK
Yes, that is why I have posted my code. The data is saved to the database but neither the success or failure events fire. Can you see where there is an error?
ignatandrei
All-Star
137682 Points
22147 Posts
Moderator
MVP
Re: I cant get a redirect to work
Mar 05, 2012 03:01 PM|LINK
fire Internet explorer.
tools , options , advanced, alert each javascript error.
( your error can be in another javascript)
calibur
Member
300 Points
124 Posts
Re: I cant get a redirect to work
Mar 05, 2012 03:03 PM|LINK
I have used Chrome debugger - its not showing any errors:
calibur
Member
300 Points
124 Posts
Re: I cant get a redirect to work
Mar 05, 2012 03:10 PM|LINK
In IE I am getting the following error:
Line: 79
Error: 'err' is undefined
I have checked the page source as well as the Jquery file - I removed 'err' and now the failure alert is displaying. Not sure why chrome isnt reporting an error
ignatandrei
All-Star
137682 Points
22147 Posts
Moderator
MVP
Re: I cant get a redirect to work
Mar 05, 2012 04:08 PM|LINK
nice. And what error it is?
calibur
Member
300 Points
124 Posts
Re: I cant get a redirect to work
Mar 05, 2012 04:15 PM|LINK
OK - so I started using Firebug and the error is :
SyntaxError: JSON.parse: unexpected character.
calibur
Member
300 Points
124 Posts
Re: I cant get a redirect to work
Mar 05, 2012 04:18 PM|LINK
And this is the json :
{"UserName":"user1","Email":"test1@test.com","Password":"password","ConfirmPassword":"password","FirstName":"user1","LastName":"test","Apartments":[{"ComplexName":"Beauparc Downs","ApartmentNo":"425"}]}ignatandrei
All-Star
137682 Points
22147 Posts
Moderator
MVP
Re: I cant get a redirect to work
Mar 05, 2012 07:19 PM|LINK
seems ok to me. What about action ? Does it gets hit?