简体   繁体   中英

call method from controller in asp.net button from view

I am new with MVC technology and I am trying to get going slowly learning this technology. I have one problem, which I think is fairly simple but I can't find any solution. I have a view with this code:

<script runat="server">

    protected void btnSubmit_Click(object sender, EventArgs e)
    {

    }
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2><%: ViewData["Message"] %></h2>
    <form id="submitForm" runat="server">
    <p>Enter URL: <asp:TextBox ID="txtOne" runat="server" />&nbsp;<asp:Button 
            ID="btnSubmit" runat="server" onclick="btnSubmit_Click" Text="Submit" />
&nbsp;<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
            ControlToValidate="txtOne" Display="Dynamic" 
            ErrorMessage="Website URL not valid." 
            ValidationExpression="http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&amp;=]*)?"></asp:RegularExpressionValidator>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
            ControlToValidate="txtOne" Display="Dynamic" ErrorMessage="Please enter URL."></asp:RequiredFieldValidator>
    </p>
    </form>
</asp:Content>

And I have the controller code where I have a method:

 public ActionResult isValidURL(string url)
        {
            if (Regex.IsMatch(url, @"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&amp;=]*)?"))
            {
            }
            return View();
        }

How can I call the isValidURL when the button is clicked and pass a parameter as well? Thanks in advance Laziale

You should use simple html or special HTML helpers , but not asp.net controls starting from <asp:

Firstly you need to remove button click handler. Then you need to name your text box with url as "url" it should equal to the name of the isValidURL() method parameter. After that make sure that your button is submitting form to the following url {your controller name}/isValidURL . And that when you will click your button, form will be submitted to the server and your isValidURL method will be executed.

What is going on behind the scenes

Very roughly, but gives basic understanding of what is going on. ASP.NET MVC uses naming convention for form and url parameters. If you are sending HTTP request via GET or POST ASP.NET MVC parses it using routing rules you've registered, runs your controller and corresponding action, eg you posted http request using GET to "http:/localhost/controller1/Action1?x=8" . In this case ASP.NET MVC will try to run Controller1 and then its method called Action1 with the following signature "Action(int x)".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM