[RESOLVED]Tab auto postback when clicked

Hello,

I have a three tabs Inbox, Sent and Deleted as shown here

 <li class="active">
                                        <a data-toggle="tab" href='@Url.Action("UserEmails", "Profile", routeValues: new { area = "Profile", s = (int)Enums.EmailStatus.Inbox })'>
                                            <i class="blue ace-icon fa fa-inbox bigger-130"></i>
                                            <span class="bigger-110">Inbox</span>
                                        </a>
                                    </li>
                                    <li>
                                        <a data-toggle="tab" href='@Url.Action("UserEmails", "Profile", routeValues: new { area = "Profile", s = (int)Enums.EmailStatus.Sent })'>
                                            <i class="orange ace-icon fa fa-location-arrow bigger-130"></i>
                                            <span class="bigger-110">Sent</span>
                                        </a>
                                    </li>
                                    
                                    <li>
                                        <a data-toggle="tab" href='@Url.Action("UserEmails", "Profile", routeValues: new { area = "Profile", s = (int)Enums.EmailStatus.Sent })'>
                                            <i class="red ace-icon glyphicon glyphicon-trash bigger-130"></i>
                                            <span class="bigger-110">Deleted</span>
                                        </a>
                                    </li>

But when i click on them they don’t post back?

this is my controller

 [HttpPost]
        public ActionResult UserEmails(int s)
        {
            try
            {
                _userEmails = GetUserEmails(s);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            return View(_userEmails);
        }

what am I doing wrong?

Your existing tabs are actually anchors (e.g. <a> tags) which are just going to navigate to a particular action (by making a GET request). If you don’t explicitly need to POST your values, the easiest approach to handling this would be to simply remove the
[HttpPost] attribute that is decorating your UserEmails action :

public ActionResult UserEmails(int s)
{
            try
            {
                _userEmails = GetUserEmails(s);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            return View(_userEmails);
}

If this isn’t going to work for your needs, then you would likely need to wrap each of your tabs with a <form> element that had it’s action attribute set to the value of your existing href attributes and it’s method attribute set to "post".

I see, I have removed the post attribute on my controller so I have the following

  [HttpGet]
        public ActionResult UserEmails() // Which is called when the view is requested.
        {
            try
            {
                _userEmails = GetUserEmails((int)Enums.EmailStatus.Inbox);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            return View(_userEmails);
        }

        public ActionResult UserEmails(int s) // Trying to call this when tab is clicked
        {
            try
            {
                _userEmails = GetUserEmails(s);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            return View(_userEmails);
        }

now when I click on the tabs It still does nothing, I apologies if I have mis-understood when you have mentioned above.

Can anyone help with this as its quite urgent.

Well there will likely be an issue with two Controller actions that share the same name. You can resolve this by just adding a bit of logic and consolidating both of the Controller Actions into a single one :

public ActionResult UserEmails(int? s) 
{
            try
            {
                // Check if a value was passed in or not
                if(s.HasValue)
                {
                       _userEmails = GetUserEmails(s.Value);
                }
                else
                {
                       _userEmails = GetUserEmails((int)Enums.EmailStatus.Inbox);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            return View(_userEmails);
}

Leave a Reply