Call post ActionMethod with parameter on ActionLink Click in ASP.Net MVC

tex
 
on Aug 30, 2021 10:52 PM
444 Views

I have two overload for method Index one without parm and the second have string searchText parm

And when click on ActionLink it take me to the first overload (without parm) 

@Html.ActionLink("value", "Index", "Home", new { searchText = "value" }, new { @class = "btn-link" })

 

public IActionResult Index()
{
    AccountDetailsViewModel _accountDetails = new AccountDetailsViewModel
    {
        Registration = new VmRegistration()
    };

    _accountDetails.IsSuccess = false;
  
    _accountDetails.ErrorDesc = "";

    return View(_accountDetails);
}

[HttpPost]
public IActionResult Index(string searchText)
{
    if (string.IsNullOrEmpty(searchText))
    return View("Index", new AccountDetailsViewModel());

    var _accountDetails = AccountDetailsLogic.GetAccountDetails(searchText.Trim());

    return View("Index", _accountDetails);
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
tex
 
on Aug 31, 2021 04:25 AM

I fixed the issue by removing the overload and use one method with optional parameter and the issue resolved 

public IActionResult Index(string searchText ="")
{
   if (string.IsNullOrEmpty(searchText))
      return View("Index", new AccountDetailsViewModel());
 
   var _accountDetails = AccountDetailsLogic.GetAccountDetails(searchText.Trim());
 
   return View("Index", _accountDetails);
}