0

Earlier I had ActionLink

 <%: Html.ActionLink("Add Race", "AddRace", 
          new {eventId = Model.EventId,fleetId=Model.SelectedFleet.ID}) %>

on my page..and it is working fine. But now my requirement is it must check if some there is some data in FooEntity then it redirect to another view otherwise it should display an alert message..

so i changed ActionLink to ahref and thinking to use $.ajax call with it..but my action AddRace is written accordingly ActionLink. but now i'm using ahref so i want to make ajaxcall which will return some boolean and based on that i'll redirect or show alert. I need to re-write AddRace Action. I'm pretty new to MVC so confuse in changing my AddRace action.

ahref:

 <a href="#" onclick='checkFleetAddedandScroing()'>Add Race</a>

Action is:

 public ActionResult AddRace(Guid eventId, Guid fleetId)
    {
        try
        {
            var model = new RaceModel { EventID = eventId, FleetID = fleetId, IsAddPHRFBFactors = IsAddPhrfbFactors(eventId, fleetId) };
            SetFleetsInViewBagForAddRace(eventId);
            return View(model);
        }
        catch (Exception ex)
        {                
            throw;
        }            
    }

Please suggest me how to do it....

Mayank Pathak
  • 3,621
  • 5
  • 39
  • 67

1 Answers1

1

You need to use the same parameters.

<a href="#" onclick='checkFleetAddedandScroing("<%= Url.Action("AddRace", 
    new {eventId = Model.EventId,fleetId=Model.SelectedFleet.ID}  %>")'>Add Race</a>

EDIT:

You don't even need to do an a tag.

 <%: Html.ActionLink("Add Race", "AddRace", 
      new {eventId = Model.EventId,fleetId=Model.SelectedFleet.ID}, 
      new {onclick="return myFunction()"}) %>

<script type="text/javascript">
    function myFunction() {
        return confirm("Click no to cancel the navigation, otherwise click yes");
    }
</script>

Then you would do your ajax call in the javascript function and return false when you want to cancel redirect.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • thanks for quick response..but i'm confused in as `ActionLink` could directly redirect me to view..but in this case redirection will be optional. like if some relative data is present is `FooEntity` then only redirect otherwise it must show an alert and return false..That's what i'm confusing in writing and in changing `Action` according to this requirement.. – Mayank Pathak Oct 27 '12 at 09:09
  • @MayankPathak - then why did your post have an a tag with different parameters? – Erik Funkenbusch Oct 27 '12 at 09:18
  • sorry, that's my mistake in asking question..that will be just a function..I've edited my question..that `js` function will make ajax call and based on bool result i have to either redirect or show an alert.. – Mayank Pathak Oct 27 '12 at 09:20
  • its still navigating even it i click cancel...' – Mayank Pathak Oct 27 '12 at 10:27
  • @MayankPathak - see my update, add the return keyword in the onclick – Erik Funkenbusch Oct 27 '12 at 10:48