Razor 表单仅提交一个输入字段
Razor form submitting only a single input field
我正在 Razor Page 中创建一个简单的表单,它将表单值发送回前端,但是当我在后端使用相同的值时,它们为空。
<form method="POST" class="form form--login">
<fieldset class="card card--login">
<legend>Login</legend>
<input aria-label="username" name="Username" type="text" title="Username can't be empty." placeholder="Username"
class="input input--text" />
<input aria-label="password" name="Password" type="password" title="Password is required." placeholder="Password"
class="input input--text" />
<input aria-label="Test" name="Test" type="datetime-local" title="Test is required." placeholder="Test"
class="input input--text" />
</fieldset>
<button type="submit" class="button button--submit">Submit</button>
</form>
@* Using this to check for values after form submission *@
@Model.Username
@Model.Password
@Model.Test
public string Username { get; set; }
public string Password { get; set; }
public string Test { get; set; }
Username = Request.Form["Username"];
Password = Request.Form["Password"];
Test = Request.Form["Test"];
Console.WriteLine(Username, Password, Test);
尝试删除 Form 关键字。
var Username = Request["Username"].ToString();
var Password = Request["Password"].ToString();
var Test = Request["Test"].ToString();
按如下方式编辑您的 PageModel:
public class IndexModel : PageModel
{
[BindProperty]
public string Username { get; set; }
[BindProperty]
public string Password { get; set; }
[BindProperty]
public string Test { get; set; }
public void OnGet()
{
}
public IActionResult OnPost()
{
return Page();
}
}
您的页面:
@page
@model IndexModel
<form method="POST" class="form form--login">
<fieldset class="card card--login">
<legend>Login</legend>
<input aria-label="username" name="Username" type="text" title="Username can't be empty." placeholder="Username"
class="input input--text" />
<input aria-label="password" name="Password" type="password" title="Password is required." placeholder="Password"
class="input input--text" />
<input aria-label="Test" name="Test" type="datetime-local" title="Test is required." placeholder="Test"
class="input input--text" />
</fieldset>
<button type="submit" class="button button--submit">Submit</button>
</form>
@Model.Username
@Model.Password
@Model.Test
测试结果:
更详细的可以了解这个article。
我正在 Razor Page 中创建一个简单的表单,它将表单值发送回前端,但是当我在后端使用相同的值时,它们为空。
<form method="POST" class="form form--login">
<fieldset class="card card--login">
<legend>Login</legend>
<input aria-label="username" name="Username" type="text" title="Username can't be empty." placeholder="Username"
class="input input--text" />
<input aria-label="password" name="Password" type="password" title="Password is required." placeholder="Password"
class="input input--text" />
<input aria-label="Test" name="Test" type="datetime-local" title="Test is required." placeholder="Test"
class="input input--text" />
</fieldset>
<button type="submit" class="button button--submit">Submit</button>
</form>
@* Using this to check for values after form submission *@
@Model.Username
@Model.Password
@Model.Test
public string Username { get; set; }
public string Password { get; set; }
public string Test { get; set; }
Username = Request.Form["Username"];
Password = Request.Form["Password"];
Test = Request.Form["Test"];
Console.WriteLine(Username, Password, Test);
尝试删除 Form 关键字。
var Username = Request["Username"].ToString();
var Password = Request["Password"].ToString();
var Test = Request["Test"].ToString();
按如下方式编辑您的 PageModel:
public class IndexModel : PageModel
{
[BindProperty]
public string Username { get; set; }
[BindProperty]
public string Password { get; set; }
[BindProperty]
public string Test { get; set; }
public void OnGet()
{
}
public IActionResult OnPost()
{
return Page();
}
}
您的页面:
@page
@model IndexModel
<form method="POST" class="form form--login">
<fieldset class="card card--login">
<legend>Login</legend>
<input aria-label="username" name="Username" type="text" title="Username can't be empty." placeholder="Username"
class="input input--text" />
<input aria-label="password" name="Password" type="password" title="Password is required." placeholder="Password"
class="input input--text" />
<input aria-label="Test" name="Test" type="datetime-local" title="Test is required." placeholder="Test"
class="input input--text" />
</fieldset>
<button type="submit" class="button button--submit">Submit</button>
</form>
@Model.Username
@Model.Password
@Model.Test
测试结果:
更详细的可以了解这个article。