簡體   English   中英

將HTML表單數據獲取到ASP.NET控制器

[英]Getting HTML form data to ASP.NET controller

因此,我有一個包含HTML登錄表單的視圖,我需要通過發布將表單中的值傳遞給控制器​​。 現在,我正在嘗試在提交表單時通過Content方法顯示用戶的用戶名,一旦我知道控制器中有用戶名,我就可以做剩下的事情。 我的問題是,當我提交表單時,沒有顯示用戶名,我已經輸出到控制台,以檢查是否正在調用控制器操作,是否沒有調用。

任何幫助,萬分感謝!

視圖

@{
    @model AdminPanel.Models.cUser
    Layout = null;
    ViewBag.Title = "Login";
}

<head>
    <link href="~/Content/Login.css" rel="stylesheet" type="text/css" />
    <link href="~/Content/Site.css" rel="stylesheet" type="text/css" />
    <link href="~/Content/bootstrap.min.css" rel="stylesheet" type="text/css" />
</head>

<body>
    <!-- Div containing the log in box parent -->
    <div class="log-in-box-parent">
        <h2>LOG IN</h2>

        <div class="log-in-form">
            <form asp-action="Login" asp-controller="Login">
                <input asp-for="Username" type="text" id="Username" placeholder="Username" /> <br />
                <input aso-for="Password" type="password" id="Password" placeholder="Password" /> <br />
                <input type="submit" id="login" value="Log in"/>
            </form>
        </div>
    </div>
</body>

控制者

using AdminPanel.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AdminPanel.Controllers
{
    public class LoginController : Controller
    {
        // GET: Login
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult Login(cUser user)
        {
            return Content($"Hello {user.Username}");
        }
    }
}

最后是我的用戶模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AdminPanel.Models
{
    public class cUser
    {
        public string Username { get; set; }
        public string Password { get; set; }
    }
}

我注意到您的Login操作僅接受POST請求,但是您的<form標記不具有method="post"屬性,該屬性將告訴表單執行POST請求(而不是GET)。 我認為您需要對此進行設置,以使其對操作方法提出正確的要求。

<form asp-action="Login" asp-controller="Login" method="post">

https://docs.microsoft.com/zh-cn/aspnet/core/mvc/views/working-with-forms也包含此示例。

感謝那些試圖幫助我的人,我在創建表單時改用了HTML Helpers來解決了這個問題。 我的編輯視圖可以在下面看到。

@model AdminPanel.Models.cUser

@{

    Layout = null;
    ViewBag.Title = "Login";
}

<head>
    <link href="~/Content/Login.css" rel="stylesheet" type="text/css" />
    <link href="~/Content/Site.css" rel="stylesheet" type="text/css" />
    <link href="~/Content/bootstrap.min.css" rel="stylesheet" type="text/css" />
</head>

<body>
    <!-- Div containing the log in box parent -->
    <div class="log-in-box-parent">
        <h2>LOG IN</h2>

        <div class="log-in-form">

            @using (Html.BeginForm("Login", "Login", FormMethod.Post))
            {
                @Html.TextBoxFor(model => model.Username, new { placeholder = "Username" })
                @Html.TextBoxFor(model => model.Password, new { placeholder = "Password" })
                <input type="submit" id="login" value="Log in"/>
            }
        </div>
    </div>
</body>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM