Home > Blockchain >  submit button is not working in asp.net MVC view
submit button is not working in asp.net MVC view

Time:04-13

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

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 >Choose the Environment</h1>
<p>
    <a asp-action="Create">
        <input type="submit" value="Enter a new connection"  />
    </a>
</p>

<div >
    @foreach (var item in Model)
    {
    <div >
        <p >@Html.DisplayFor(modelItem => item.env_name)</p>
        <ul >
            <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 >Edit</button>
        </a>
        <a asp-action="Details" asp-route-id="@item.id">
            <button >Details</button>
        </a>
        <a asp-action="Delete" asp-route-id="@item.id">
            <button >Delete</button>
        </a>
        <br />

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

        

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


View: (Run.cshtml):

@model IEnumerable<NEW_Tools.Models.LocalEnvGuide>

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


<!DOCTYPE html>

<div >
    <h1 >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 >
    <a asp-action="Index">
        <input type="submit" value="Back to connections history"  />
    </a>
</div>


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

CodePudding user response:

<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"  style="background-color:limegreen" />
</form>

CodePudding user response:

Purpose of ValidateAntiForgeryToken is to prevent cross-site request forgery attacks.

Add below line on your view

@Html.AntiForgeryToken()
  • Related