MVC:replacing HTML.ActionLink with ahref
-
14-07-2021 - |
Question
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....
Solution
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.