Asp mvc Ajax.BeginForm return See or value, confused on return types
source link: https://www.codesd.com/item/asp-mvc-ajax-beginform-return-see-or-value-confused-on-return-types.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Asp mvc Ajax.BeginForm return See or value, confused on return types
If the the user is not using javascript, I will refresh the view with a RedirectToAction after an IsAjaxRequest check. This works when I have a user contrl in the page and return partial view.
BUt when I just want to return a value, say int, and also still want to use RedirectToAction when not an ajax request, how do i set up the method in the controller?
public ActionResult...
(if ajax request) Return View (new{value=5})?
(if not) redirecttoAction ("view");
not working.
You need to use the JsonResult.
if (IsAjaxRequest()) {
return Json(new { value = 5 });
} else {
return RedirectToAction("parentAction");
}
It will serialize the anonymous object in to a JSON result. Or if you really want to just return an "int", which isn't really common AJAX you should use the ContentResult
.
Related Articles
asp.net mvc Ajax.BeginForm
MVC Ajax.BeginForm Replaces Strange Behavior
Ajax.BeginForm returns Json for the message
asp.net mvc Ajax.BeginForm clone
ASP.NET MVC Ajax.BeginForm does not work
ASP .NET MVC AJAX BeginForm InsertionMode.Replace does not work correctly
Asp.Net MVC Ajax.BeginForm does not show up via Ajax
ASP.Net Mvc Ajax.BeginForm with a submission button trips twice
Asp.net mvc ajax.beginform file upload
MVC Ajax.BeginForm Insert mode
MVC Ajax Beginform hides submission button on call ajax
ASP.NET AJAX.BeginForm sends multiple requests
Using DI and IOC with ASP.MVC 3 How to configure my controllers to solve new types correctly?
The basic ASP MVC AJAX Json query returns null
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK