locked
ASP.NET MVC Clicking the Button Without Refreshing the Page RRS feed

  • Question

  • User856008996 posted

    Hello,

    Can you help me with my painful transition to ASP.NET MVC? Now I fill out a form and register to the database. The page is refreshed when the button on the form is clicked. I don't want it to be renewed. As with ASP.NET Webform, is there no solution in the logic of UpdatePanel & Trigger> AsyncPostBack?

    I found Html.ActionLink as a solution but learned that it is not suitable for the form.

    I am still using ADO.NET, I want to progress first in ASP.NET MVC and then switch to updated technologies. MVC was enough to confuse me, it has already increased :)

    [HttpPost]
            public async Task<IActionResult> QuoteMessageSend(Quote quote)
            {
                if (ModelState.IsValid)
                {
                    string connectionString = Configuration["ConnectionStrings:DefaultConnection"];
                    using (MySqlConnection connection = new MySqlConnection(connectionString))
                    {
    
                        using (MySqlCommand cmd = new MySqlCommand("INSERT INTO quotes(FullName, Email, Phone, Budget, Message) VALUES(@fn, @em, @ph, @bdg, @msg)", connection))
                        {
                            connection.Open();
                            cmd.Parameters.AddWithValue("@fn", quote.FullName);
                            cmd.Parameters.AddWithValue("@em", quote.Email);
                            cmd.Parameters.AddWithValue("@ph", quote.Phone);
                            cmd.Parameters.AddWithValue("@bdg", quote.Budget);
                            cmd.Parameters.AddWithValue("@msg", quote.Message);
                            await cmd.ExecuteNonQueryAsync();
                            connection.Close();                        
                        }
                    }
                    return View("Index");
                }
                else
                {
                    return View("Index");
                }           
            }
    @using (Html.BeginForm("QuoteMessageSend", "Home", FormMethod.Post))
                {
    
                    <div asp-validation-summary="ModelOnly"></div>
                    <p>
                        <input asp-for="FullName" placeholder="Full Name" class="contact-item" />
                        <span asp-validation-for="FullName"></span>
                    </p>
                    <p>
                        <input asp-for="Email" placeholder="Email" class="contact-s-item" />
                        <span asp-validation-for="Email"></span>
                    </p>
                    <p>
                        <input asp-for="Phone" placeholder="Phone" class="contact-s-item" />
                        <span asp-validation-for="Phone"></span>
                    </p>
                    <p>
                        <select asp-for="Budget" class="contact-l-item">
                            <option value="" disabled selected>Budget</option>
                            <option value="200-1000">€100 - €500</option>
                            <option value="200-1000">€500 - €3000</option>
                            <option value="200-1000">€3000 - €10000</option>
                            <option value="200-1000">€10000+</option>
                        </select>
                        <span asp-validation-for="Budget"></span>
                    </p>
                    <p>
                        <textarea asp-for="Message" placeholder="Your Ideas" class="contact-m-item"></textarea>
                        <span asp-validation-for="Message"></span>
                    </p>
                    <button id="submit" type="submit">Submit Now</button>
                }
    

    Thursday, May 21, 2020 5:06 PM

All replies