Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

submit button is not working in asp.net MVC view

I’m trying to run C# code with the submit button in MVC but I get an error.
When I click ‘Connect’ button I get this error:

error message:

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The model:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace NEW_Tools.Models
{
    public class LocalEnvGuide
    {
        public int id { get; set; }

        [Required(ErrorMessage = "The path field is required")]
        public string mvc_cf_path { get; set; }

        [Required(ErrorMessage = "The path field is required")]
        public string es_cf_path { get; set; }

        [Required(ErrorMessage = "The environment name field is required")]
        public string env_name { get; set; }

        [Required]
        public bool enable_tasker { get; set; }
    }
}

Controller:(LocalEnvGuides.cs)

...
 // POST: LocalEnvGuides/Run
        [HttpPost, ActionName("Run")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> RunConfirmed(int id)
        {
            var localEnvGuide = await _context.LocalEnvGuide.FindAsync(id);
            string machine_name = Environment.MachineName.ToString();
            string env_name = localEnvGuide.env_name;
            string mvc_cf_path = localEnvGuide.mvc_cf_path;
            string es_cf_path = localEnvGuide.es_cf_path;
            Run.Main(machine_name, env_name, mvc_cf_path, es_cf_path, true);

            ViewBag.Message = string.Format("Hello {0}.", machine_name);
            return View();
        }

View: (Index.cshtml):

@model IEnumerable<NEW_Tools.Models.LocalEnvGuide>

@{
    ViewData["Title"] = "Index";
}
<link rel="stylesheet" href="~/css/StyleTable.css">

<h1 class="text-center">Choose the Environment</h1>
<p>
    <a asp-action="Create">
        <input type="submit" value="Enter a new connection" class="btn btn-primary" />
    </a>
</p>

<div class="pricing-box-container">
    @foreach (var item in Model)
    {
    <div class="pricing-box text-center">
        <p class="price">@Html.DisplayFor(modelItem => item.env_name)</p>
        <ul class="features-list">
            <li>@Html.DisplayFor(modelItem => item.mvc_cf_path)</li>
            <li>@Html.DisplayFor(modelItem => item.es_cf_path)</li>
            <li>Tasker Status:     @Html.DisplayFor(modelItem => item.enable_tasker)</li>
        </ul>
        <a asp-action="Edit" asp-route-id="@item.id">
            <button class="btn-primary">Edit</button>
        </a>
        <a asp-action="Details" asp-route-id="@item.id">
            <button class="btn-primary">Details</button>
        </a>
        <a asp-action="Delete" asp-route-id="@item.id">
            <button class="btn-primary">Delete</button>
        </a>
        <br />

        <a asp-action="Run" asp-route-id="@item.id">
            <input type="submit" value="Connect" class="btn btn-primary" style="background-color:limegreen" />
        </a>    

        

        <a asp-action="Disconnect" asp-route-id="@item.id">
            <input type="submit" value="Disconnect" class="btn btn-primary" style="background-color:red" />
        </a>
    </div>
    }


View: (Run.cshtml):

@model IEnumerable<NEW_Tools.Models.LocalEnvGuide>

@{
    ViewData["Title"] = "Index";
}


<!DOCTYPE html>

<div class="text-center">
    <h1 class="display-4">You are connected to the environment!</h1>
</div>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Run</title>
</head>
<body>
    @if (ViewBag.Message != null)
    {
        <script type="text/javascript">
            window.onload = function () {
                alert("Congratulations, you have a new connection!");
            };
        </script>
    }
</body>
</html>
<br />

<div class="text-center">
    <a asp-action="Index">
        <input type="submit" value="Back to connections history" class="btn btn-primary" />
    </a>
</div>


I would like to get your help with that please, thanks!
(if you need more details, comment and I will add)

>Solution :

<a> link can only send httpget request,you can try to use form to replace it:

<form method="post" asp-action="Run" asp-route-id="1">
    <input type="submit" value="Connect" class="btn btn-primary" style="background-color:limegreen" />
</form>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading