Answered by:
How to return to another MVC view or MVC action from web API controller ?

Question
-
User-1846805900 posted
Hi
i have use web api controller to save data into database throw knocoutjs and it works fine, but i need to redirect to other view after inserting data on database and i can't use return view or redirect to action in web api
as example i need to redirect to MVC action "Index" on MVC controller "Doctors"
so please how can i do that ?
readonly GPSContext db = new GPSContext(); public IHttpActionResult AddLoc(Maps_Pos postoadd) { if (ModelState.IsValid) { // save to database db.Maps_Pos.Add(postoadd); db.SaveChanges(); return Ok(); } else { return BadRequest(ModelState); } }
Thursday, May 21, 2015 10:46 AM
Answers
-
User1293388710 posted
Check ou this thread...
http://forums.asp.net/t/2009161.aspx?Redirect+to+a+page+from+Web+API+APIController
Answer by Fuxiang Zhang - MSFT
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, May 21, 2015 12:51 PM -
User1644755831 posted
Hello a.amin,
Assuming you are calling this from javascript using knockout js code. in the success call of your ajax call just use window.location to redirect to index page.
Please see: http://www.w3schools.com/js/js_window_location.asp
Example:
$.ajax({ url: 'http://localhost:8080/api/AddLoc', type: 'GET', dataType: 'json', success: function (data) { window.location = '@Url.Action("Inndex", "Doctors")'
//or just use url directly if this is external js like http://localhost/Doctors/Index }, error: function (x, y, z) { alert(x + '\n' + y + '\n' + z); } });Hope this helps.
With Regards,
Krunal Parekh
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 22, 2015 5:48 AM
All replies
-
User1293388710 posted
Check ou this thread...
http://forums.asp.net/t/2009161.aspx?Redirect+to+a+page+from+Web+API+APIController
Answer by Fuxiang Zhang - MSFT
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, May 21, 2015 12:51 PM -
User-1846805900 posted
Check ou this thread...
http://forums.asp.net/t/2009161.aspx?Redirect+to+a+page+from+Web+API+APIController
Answer by Fuxiang Zhang - MSFT
hi
i see it, but i don't understand how can i make it redirect to "/doctors/Index" as example ?
Thursday, May 21, 2015 6:01 PM -
User1644755831 posted
Hello a.amin,
Assuming you are calling this from javascript using knockout js code. in the success call of your ajax call just use window.location to redirect to index page.
Please see: http://www.w3schools.com/js/js_window_location.asp
Example:
$.ajax({ url: 'http://localhost:8080/api/AddLoc', type: 'GET', dataType: 'json', success: function (data) { window.location = '@Url.Action("Inndex", "Doctors")'
//or just use url directly if this is external js like http://localhost/Doctors/Index }, error: function (x, y, z) { alert(x + '\n' + y + '\n' + z); } });Hope this helps.
With Regards,
Krunal Parekh
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 22, 2015 5:48 AM