将参数从cshtml解析为cshtml.cs中的操作

时间:2019-08-02 03:23:37

标签: c# asp.net-mvc asp.net-core

因此,我试图在asp.net核心中为登录表单创建一个简单的令牌承载程序,结果陷入了僵局。

如您在我的代码中所见, 我有两个用于输入用户名和密码的输入,我想将这两个输入解析为cshtml.cs中的参数

这是我的cshtml

@page
@model AtanaAspNetCore.Pages.LoginModel
@{
    ViewData["Title"] = "Login";
}

<div style="border:1px solid #bbb9b9; border-radius:10px;">
    <form method="post" asp-action="Login" asp-controller="Login">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="p-4 border rounded" style="padding:30px;">
            <div class="form-group row">
                <label class="col-form-label col-lg-2">Username</label>
                <div class="col-lg-10">
                    <input asp-for="Username" type="text" class="form-control col-12" name="Username" id="Username" />
                </div>
                <span class="text-danger" />
            </div>

            <br />

            <div class="form-group row">
                <label class="col-form-label col-sm-2">Password</label>
                <div class="col-sm-10">
                    <input asp-for="Password" type="password" class="form-control" name="Password" id="Password" />
                </div>
            </div>

            <div class="form-group row">
                <input name="token" type="hidden" value="token" />
            </div>

            <br />

            <div class="form-group">
                <input type="submit" name ="Login" value="Login" asp-route-Username=@Model.Username asp-route-Password=@Model.Password class="btn btn-primary btn-sm" />
                <a asp-page="Login" class="btn btn-success btn-sm">Cancel</a>
            </div>
        </div>
    </form>

</div>

这是我的cshtml.cs

public class LoginModel : PageModel
    {
        private GlobalDbContext _context;
        IAuthService AuthService;

        [TempData]
        public string Message { get; set; }

        public string Username { get; set; }

        public string Password { get; set; }

        public LoginModel(GlobalDbContext context, IAuthService AuthService)
        {
            this._context = context;
            this.AuthService = AuthService;
        }

        public IActionResult SignIn(LoginModel model)
        {
            if (!ModelState.IsValid)
            {
                Message = "Wrong Username / Password";
                return BadRequest(ModelState);
            }

            var token = AuthService.SignIn($"{ model.Username }", $"{ model.Password }");
            if (token == null)
            {
                Message = "Token is null";
                return BadRequest();
            }
            else
            {
                Message = "Logged in successfully!";
                return RedirectToPage("Index");
            }

        }

        public IActionResult RefreshToken(string token)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);
            var auth = AuthService.RefreshToken(token);
            if (string.IsNullOrEmpty(auth))
                return Unauthorized();
            return RedirectToPage("Index");
        }

        public IActionResult ApiVersion()
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);
            return RedirectToPage("Index");
        }
        public void OnGet()
        {

        }

        public IActionResult OnPost(LoginModel model)
        {
            if (!ModelState.IsValid)
            {
                Message = "Wrong Username / Password";
                return Page();
            }

            var token = AuthService.SignIn($"{ model.Username }", $"{ model.Password }");
            if (token == null)
            {
                Message = "Token is null";
                return Page();
            }
            else
            {
                Message = "Logged in successfully!";
                return RedirectToPage("Index");
            }
        }
    }

您可以逐步指导我如何做吗? 我是新来的。

预先感谢

1 个答案:

答案 0 :(得分:0)

您可以使用asp-route-something获取路线参数。如您所见,在删除按钮中,我们传入了asp-route-id="@contact.Id"。因此,在我们的OnPostDeleteAsync中,我们可以接收ID数据

@page
@model RazorPagesContacts.Pages.IndexModel
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

<h1>Contacts</h1>
<form method="post">
    <table class="table">
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var contact in Model.Customers)
            {
                <tr>
                    <td>@contact.Id</td>
                    <td>@contact.Name</td>
                    <td>
                        <a asp-page="./Edit" asp-route-id="@contact.Id">edit</a>
                        <button type="submit" asp-page-handler="delete" 
                                asp-route-id="@contact.Id">delete</button>
                    </td>
                </tr>
            }
        </tbody>
    </table>

    <a asp-page="./Create">Create</a>
</form>

在后面的代码中

public class IndexModel : PageModel
{
    private readonly AppDbContext _db;

    public IndexModel(AppDbContext db)
    {
        _db = db;
    }

    public IList<Customer> Customers { get; private set; }

    public async Task OnGetAsync()
    {
        Customers = await _db.Customers.AsNoTracking().ToListAsync();
    }

    public async Task<IActionResult> OnPostDeleteAsync(int id)
    {
        var contact = await _db.Customers.FindAsync(id);

        if (contact != null)
        {
            _db.Customers.Remove(contact);
            await _db.SaveChangesAsync();
        }

        return RedirectToPage();
    }
}