Aniebiet
Aniebiet

Reputation: 39

Why does asp-action not hit the corresponding method ASP.NET Core MVC

I am new to .NET Core and learning the ropes. I have been stuck for 2 days trying to solve the puzzle.

I have the following code in my index.cshtml:

<td>
    <a class="btn btn-success btn-sm mx-1" asp-controller="MemberList" asp-action="PickAm" asp-route-id="@item.MemberID">Select</a>
</td>

Also this in the corresponding MemberlistController:

public class MemberListController : Controller
{
    // private readonly PrMagicContext _context;
    private readonly iMembersRepository _mr;

    public MemberListController(iMembersRepository context)
    {
        _mr = context;
    }

    public IActionResult Index()
    {
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult Index(string SearchString)
    {
        IEnumerable<MemberVM> memlist = _mr.FindMemberList(SearchString);
        return View(memlist);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult PickAm(long? MemberID)
    {
        if(MemberID == null || MemberID == 0)
        {
            return NotFound();
        }

        IEnumerable<MemberVM> memlist = _mr.FindMemberListbyId((long)MemberID);
        MemberVM memid;

        if(memlist.Count() > 0)
        {
            //Session["MemID"] = memlist.First;
            memid = memlist.FirstOrDefault();
        }
        
        return View (memlist);
    }
}

Issue is when I run the code and click on the select button, it doesn't hit/run the method above. The Index method is hit from the view but the PickAm isn't.

Thank you in advance.

Edit: I changed the [HttpPost] to [HttpGet] still won't hit the Pickam method.

Below is the error shown from Console of browser diagnostics

The character encoding of the plain text document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the file needs to be declared in the transfer protocol or file needs to use a byte order mark as an encoding signature.

Upvotes: 0

Views: 1487

Answers (2)

Aniebiet
Aniebiet

Reputation: 39

Thank you guys, you all gave me pointers. @Jacob Barnes answer pointed to to how I should tinker my code. Here is my answer for the future.

I changed my Index.cshtml to

<form asp-controller="MemberList" asp-action="PickAm">
            <table class="table table-striped table-hover table-responsive table-condensed" style="width:100%">
                <thead>
                    <tr>
                        <th>
                            @Html.DisplayNameFor(model => model.fname)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.Sname)
                        </th>

                        <th>
                            @Html.DisplayNameFor(model => model.ForceNo)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.CoopNo)
                        </th>

                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var item in Model)
                    {
                        <tr>
                            <td>
                                @Html.DisplayFor(modelItem => item.fname)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.Sname)
                            </td>

                            <td>
                                @Html.DisplayFor(modelItem => item.ForceNo)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.CoopNo)
                            </td>

                            <td>
                                <button class="btn btn-success btn-sm mx-1" type="submit" name="MemberID" value="@item.MemberID">Select</button>
                                
                        </tr>
                    }
                </tbody>
            </table>
            </form>

Note the form tag added and the changes done to the Select button.

And the PickAm method turned to

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult PickAm(long MemberID)
        {
            
            IEnumerable<MemberVM> memlist = _mr.FindMemberListbyId(MemberID);
             long memid;
            if(memlist.Count() > 0)
            {
                //Session["MemID"] = memlist.First;
                memid = memlist.FirstOrDefault().MemberID;
            }
            
            return View (memlist);
        }

Upvotes: 0

jbarnes
jbarnes

Reputation: 86

Links create GET requests. You have marked your PickAm() method with [HttpPost], so there is no PickAm() GET method.

You can either change the [HttpPost] above PickAm() to [HttpGet] , or you can use a form in your index.cshtml page.

The accepted answer on this question says the same thing and gives an example of a form.

EDIT:

Just tested to make sure I'm not crazy and an <a> tag will definitely not hit a method marked [HttpPost], so the [HttpGet] is necessary. The character encoding looks like a separate issue. Have you declared the character encoding in your views? It would look something like <meta content="text/html; charset=utf-8" />.

Also, just to confirm, have you put a break point right inside PickAm() and it doesn't hit it at all?

Upvotes: 1

Related Questions