Home > Mobile >  Calling ActionResult from PartialView never hits
Calling ActionResult from PartialView never hits

Time:08-03

Pretty new with MVC and going through a few tutorials. Have done the following:

Add a controller called CustomerController. Add 2 methods

    public ActionResult Render()
    {
        // Go to a third party WebAPI and get the results in a List
       return PartialView("CustomerList", custList);
    }

    public ActionResult SomeTest()
    {
        Response.Redirect("Somepage");
    }

I then add a page (LandingView.cshtml) and create a PartialView called CustomerList and add the below code to the LandingView page

@Html.Action("Render", "Customer")

When i view this page it renders the page with a list of customers. The HTML for the PartialView is

@using (Html.BeginForm("SomeTest", "Customer"))
{

    <div >
        @foreach (var i in Model)
        {
            <a href="@i.Url">
                <div >@i.Title</div><br />
                <div >@i.Price.ToString("C")</div>
            </a>
             
            <input type="button" id="btnGo" value="Go" />
        }
    </div>
}

When i click the button it never hits the SomeTest method? In debug mode i have put a breakpoint on Render and SomeTest, Render hits on page load but when clicking Go it never hits the SomeTest method?

What am i missing here?

CodePudding user response:

Set the 'type' attribute value of the input element to "submit" not "button". This will trigger the form submission on click.

<input type="submit" id="btnGo" value="Go" />

You may experience some build errors because the SomeTest() controller method is expecting a return value of type ActionResult.

  • Related