0

I am posting a form using Ajax.BeginForm due to the fact that I have some methods running in my controller's POST method which checks to see whether the proposed record is valid in terms of what already exists in the table.

// POST: Electricity
[HttpPost]
public ActionResult CreateSite(ElectricitySiteViewModel newElectricitySiteViewModel)
{
    var dbElectricitySite = AutoMapper.Mapper.Map<ElectricitySite>(newElectricitySiteViewModel);

    if (_service.DoesSiteExist(newElectricitySiteViewModel.SiteNumber)) // site already exists, check balance of split
    {
        if (_service.DoesSiteBalance(newElectricitySiteViewModel.SiteNumber, newElectricitySiteViewModel.SplitPercentage)) // if it balances, create and redirect
        {
            _service.CreateElectricitySite(dbElectricitySite);
            return JavaScript("window.location = 'http://www.google.co.uk'");
        }

        else // doesnt balance, return to create view
        {
            ViewBag.DoesNotBalance = true;
            return View();
        }
    }

    else //site does not exist, create it and redirect
    {
        _service.CreateElectricitySite(dbElectricitySite);
        return JavaScript("window.location = 'http://www.google.co.uk'");
    }         
}

I am using the ViewBag as a flag to show the user that the split they have attempted contradicts with the site record already held, so I can show an additional message without having to do a post back.

What's happening however is when the POST is valid, and the record created, I'm trying to force the browser to postback to google by doing:

return JavaScript("window.location = 'http://www.google.co.uk'");

Which I have used successfully before, but this time I'm seeing this:

enter image description here

EDIT

Following @JamieD77 's comments, I instead added an OnSuccess parameter to my Ajax.BeginForm to display a message as a test:

@using(Ajax.BeginForm(new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "updatearea", OnSuccess = "Test" }))

and added a script to my page:

@section DialogScripts{
    <script type="text/javascript">

        function Test(data) {
            switch (data.result) {
                case 1:
                    alert(data.result)
                    break;
                case 0:
                    alert(data.result)
                    break;
                case -1:
                    alert(data.result)
                    break;
                default:
                    alert(data.message);
            }
        }

    </script>
}

And had my controller return:

return Json(new { result = 0, message = "Record created." });

But I get the same thing, the JSON is just output to the browser, much like the screen shot from before, but this time its JSON.

What is going on here, am I missing a library perhaps?

JsonStatham
  • 9,770
  • 27
  • 100
  • 181
  • doesn't seem like a good use of `Ajax.BeginForm` you should just use regular `Html.BeginForm` and return `Redirect("http://www.google.co.uk")` if you want to redirect to google – JamieD77 Jan 18 '16 at 16:57
  • to elaborate, The `Ajax.BeginForm` is useful when you do not want to redirect after successful postback, but intend to stay on the same page regardless. It just over complicates things otherwise – JamieD77 Jan 18 '16 at 17:02
  • Yes I know, but I want the user to click the submit button, and find out asynchronously whether or not their site will balance, so they can adjust the percentage without having to do a postback to find out. – JsonStatham Jan 18 '16 at 17:03
  • That aside, I want to know why return JavaScript("window.location = 'http://www.google.co.uk'"); is not giving me the redirect as I know it should. – JsonStatham Jan 18 '16 at 17:04
  • If I type in window.location = 'Index'; on the chrome dev tools console window, it redirects me to that view perfectly. – JsonStatham Jan 18 '16 at 17:06
  • Your best bet is to just handle the form.submit event and check the site will balance and return a json result with a message.. then submit the form manually or show the message – JamieD77 Jan 18 '16 at 17:09
  • See my update @JamieD77 – JsonStatham Jan 18 '16 at 17:39
  • this blog mentions some of the things you need to get this to work.. http://kayteelynne.com/mvc-5-ajax-beginform-doesnt-work-ootb/ pay extra attention to the web.config settings – JamieD77 Jan 18 '16 at 18:12
  • Using ajax when you ultimately want to redirect does not make sense. Using a `[Remote]` attribute, or just using a ` –  Jan 18 '16 at 23:05

1 Answers1

0

After finding this question/answer:

How to return Javascript from an ASP.NET 5 MVC 6 controller action

I changed:

return JavaScript("window.location = 'http://www.google.co.uk'");

To:

return Content("<script language='javascript' type='text/javascript'>'http://www.google.co.uk'</script>");

And it worked. In terms of my web.config, MVC version, I'm on:

 <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
      </dependentAssembly>

Which was the same version as the project that I was previously successfully using return Javascript

So I'm not sure what the problem was?

Community
  • 1
  • 1
JsonStatham
  • 9,770
  • 27
  • 100
  • 181