0

In ASP.NET Core I am trying to pass a value from ajax to view, I have an action that passes an int value to an ajax call, and from there, ajax tells my view to blink or not. Here is the controller

public ActionResult GetCounselingCount()
{
        var result = 0;
        try
        {
            var Counseling = _context.TableName.Where(f => f.FolderStatusId == 12)
               .Where(f => f.IsActive == true).Count();

            if (Counseling != 0)
            {
                result = Counseling;
            }
        }
        catch (Exception)
        {
            throw;
        }
        return Json(result);
}

result is passed to my ajax call

$.ajax({
    dataType: "json",
    url: window.location.origin + "/ControllerName/GetCounselingCount",
    success: function (result) {
        if (result != 0) {
            $('#phaSection').addClass("blinking");
        }
        else {
            if ($('#phaSection').hasClass("blinking")) {
                $('#phaSection').removeClass("blinking");
            }
        }
    }
});

This works fine when I pass <li id="phaSection"> to my view,

<li id="phaSection" class="nav-item @((ViewContext.RouteData.Values["Controller"].ToString() == "ControllerName") && (ViewContext.RouteData.Values["Action"].ToString() == "Counseling") ? "active" : "")">
    <a class="nav-link" asp-controller="ControllerName" asp-action="Counseling">
        <i class="fas fa-book-reader"></i>
        <span>Counseling</span>
    </a>
</li>

This causes <span>Counseling</span> to blink as expected, however my question is how can I pass the int value result from my ajax to be displayed in my view? Instead of my view <span>Counseling</span>, I want it to be

<span>Counseling + "value"</span>

1 Answer 1

2

You can identify the <span> within your current target element and set its text:

$('#phaSection span').text('Counseling + ' + result);

Or give the <span> its own id:

<span id="someID">Counseling</span>

And target that directly like you do for the containing element:

$('#someID').text('Counseling + ' + result);

Either way, what you're looking to do is not "pass an int to a view" but rather just set the text of an element.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.