12

Asp mvc Ajax.BeginForm return See or value, confused on return types

 2 years ago
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.
neoserver,ios ssh client

Asp mvc Ajax.BeginForm return See or value, confused on return types

advertisements

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

I'm having some difficulties with Ajax.BeginForm I have something like this in a view <% using (Ajax.BeginForm("ActionName", null , null, new { id = "FormName" })) {%> <input type="hidden" value = '<%= Html.Encode

MVC Ajax.BeginForm Replaces Strange Behavior

In a partial view, I am using MVCs Ajax.Beginform like followed: <div id="divToReplace"> @using (Ajax.BeginForm("Action", "Controller, new AjaxOptions { InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace, UpdateTarget

Ajax.BeginForm returns Json for the message

I am using jquery unobtrusive ajax and MVC Ajax.Beginform() to validate a form via C# server side. I'm always replacing the form with itself. It's all working fine, but I am wondering: Let's say my form is triggering a "save to database" action

asp.net mvc Ajax.BeginForm clone

I'm using asp.net mvc ajax. The partial view is using Ajax.BeginForm (just an example): <div id="divPlaceholder"> <% using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "divPlaceholder" })) { %> ... asp.net mvc control

ASP.NET MVC Ajax.BeginForm does not work

<script src="../../Scripts/MicrosoftAjax.debug.js" type="text/javascript"></script> <script type="text/javascript"> function loginOK() { var item = document.getElementById('statusLabel'); item.innerHTML = &q

ASP .NET MVC AJAX BeginForm InsertionMode.Replace does not work correctly

I can see the result when entering anything in text field. The result does go to the DIV I want. However, I look at the source code for the page, I don't see the replacement element. For example, I enter 'aaaaaaaaaaaaaaaa', click submit button, I see

Asp.Net MVC Ajax.BeginForm does not show up via Ajax

I've got my form as follows <div id="contact-form" class="hidden" title="Online Request Form"> @Using (Ajax.BeginForm("Contact", "Main", Nothing, New AjaxOptions With {.UpdateTargetId = "status&

ASP.Net Mvc Ajax.BeginForm with a submission button trips twice

When using Ajax.BeginForm("Index", new AjaxOptions(...)) with a submit button. It calls the action method two times. What could be the reason? When I replace Ajax.BeginForm with Html.BeginForm it works fine as expected. @using (Ajax.BeginForm(ne

Asp.net mvc ajax.beginform file upload

is there a way i can file upload using ajax.beginform as i am using jquery dialog and can't use html.beginform Basically i am implmenting a sort of wizard for initializing website on jquery dialog which need to have ajax thereThere may be issues with

MVC Ajax.BeginForm Insert mode

Web Config: <add key="ClientValidationEnabled" value="true"/> <add key="UnobtrusiveJavaScriptEnabled" value="true"/> JavaScript referenced files: <script src="@Url.Content("~/Scripts/jquery-

MVC Ajax Beginform hides submission button on call ajax

I have a shoppingcart control that lists the items in cart. Each row has a delete submit button that is wrapped around ajax.beginform that will call the controller, delete the item in cart and render the shoppingcart as partialview. The ajax and dele

ASP.NET AJAX.BeginForm sends multiple requests

Im relatevely new with Asp.net MVC3, I have a form handled with Ajax, like this: @using (Ajax.BeginForm("dtjson", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "detalle_tarifa", OnSuccess = "exito2", OnBeg

Using DI and IOC with ASP.MVC 3 How to configure my controllers to solve new types correctly?

I've been reading Mark Seemann's book Dependency Injection in .NET and his blog post regarding DI Composition Roots. And I'm in the process of refactoring my ASP.MVC 3 application Container calls out of my controllers and into a compositional root. I

The basic ASP MVC AJAX Json query returns null

I have an MVC application with a controller named Angular (I use AngularJS as well), which has an action called GetQuestion. That action returns a JsonResult which looks like this (grabbed from Chrome): {"game":{"Title":"Diablo II

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK