C# 是否可以通过检查表单提交中的单选按钮值来重定向不同的页面? 登录 用户登录 @Model.Msg 电子邮件地址 密码 选择您是医生还是患者:
医生C# 是否可以通过检查表单提交中的单选按钮值来重定向不同的页面? 登录 用户登录 @Model.Msg 电子邮件地址 密码 选择您是医生还是患者:,c#,html,asp.net,C#,Html,Asp.net,医生 患者 使用制度; 使用System.Collections.Generic; 使用System.Linq; 使用System.Threading.Tasks; 使用Microsoft.AspNetCore.Http; 使用Microsoft.AspNetCore.Mvc; 使用Microsoft.AspNetCore.Mvc.RazorPages; 使用远程医疗模式; 名称空间b.Pages { 公共类LogInModel:PageModel { [BindProperty] 公共字符串用
患者
使用制度; 使用System.Collections.Generic; 使用System.Linq; 使用System.Threading.Tasks; 使用Microsoft.AspNetCore.Http; 使用Microsoft.AspNetCore.Mvc; 使用Microsoft.AspNetCore.Mvc.RazorPages; 使用远程医疗模式; 名称空间b.Pages { 公共类LogInModel:PageModel { [BindProperty] 公共字符串用户名{get;set;} [BindProperty] 公共字符串密码{get;set;} 公共字符串Msg{get;set;} 公共互联网 { } 公共IActionResult OnPost(字符串子项) { 使用(var context=new HealthProjectContext()) { 尝试 { var query=来自context.Patients中的st 其中st.Email==用户名 选择st.密码; string check2=query.FirstOrDefault(); if(Password.Equals(check2.Trim())) { HttpContext.Session.SetString(“用户名”,Username); 返回Topage(“欢迎”); } 其他的 { Msg=“无效”; 返回页(); } } 抓住 { Msg=“无效”; 返回页(); } } } } }
这段代码来自我正在开发的网站的一个Razorpage。代码当前设置为将用户发送到一个常规登录页面的位置。我想允许用户点击单选按钮来区分患者和医生,并将他们发送到相应的网页。我对asp.net相当陌生,所以我不是100%有可能做到这一点。您可以向模型添加
Gender
属性,以便通过在OnPost
中检查该值来重定向不同的页面,类似于:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Login</title>
</head>
<body>
<h3>User Login</h3>
@Model.Msg
<form method="post" asp-page="Login">
<table>
<tr>
<td>Email Address</td>
<td><input type="text" asp-for="@Model.Username" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" asp-for="@Model.Password" /></td>
</tr>
<tr>
<td>
Select whether you are a doctor or patient:
<p></p>
<input type="radio" id="doc" name="gender" value="Doctor">
<label for="doc">Doctor</label><br>
<input type="radio" id="pat" name="gender" value="Patient">
<label for="pat">Patient</label><br />
<tr>
<td> </td>
<td><input type="submit" value="Login" name="sub"/></td>
</tr>
</table>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using TeleHealthB.Models;
namespace TeleHealthB.Pages
{
public class LogInModel : PageModel
{
[BindProperty]
public string Username { get; set; }
[BindProperty]
public string Password { get; set; }
public string Msg { get; set; }
public void OnGet()
{
}
public IActionResult OnPost(string sub )
{
using (var context = new HealthProjectContext())
{
try
{
var query = from st in context.Patients
where st.Email == Username
select st.Password;
string check2 = query.FirstOrDefault();
if (Password.Equals(check2.Trim()))
{
HttpContext.Session.SetString("Username", Username);
return RedirectToPage("Welcome");
}
else
{
Msg = "Invalid";
return Page();
}
}
catch
{
Msg = "Invalid";
return Page();
}
}
}
}
}
单选按钮也应该是:
namespace TeleHealthB.Pages
{
public class LogInModel : PageModel
{
[BindProperty]
public string Username { get; set; }
[BindProperty]
public string Password { get; set; }
[BindProperty]
public string Gender { get; set; } //--> This is a new property
public string[] Genders = new[] { "Doctor", "Patient" };}
public string Msg { get; set; }
public void OnGet()
{
}
public IActionResult OnPost(string sub )
{
using (var context = new HealthProjectContext())
{
try
{
var query = from st in context.Patients
where st.Email == Username
select st.Password;
string check2 = query.FirstOrDefault();
if (Password.Equals(check2.Trim()))
{
HttpContext.Session.SetString("Username", Username);
//You can redirect pages by checking Gender values
if(Gender == "Doctor")
{
return RedirectToPage("Welcome_Doctor");
}
else if(Gender == "Patient")
{
return RedirectToPage("Welcome_Patient");
}
else
{
Msg = "Invalid Gender";
return Page();
}
}
else
{
Msg = "Invalid";
return Page();
}
}
catch
{
Msg = "Invalid";
return Page();
}
}
}
}
}
@foreach(Model.Genders中的变量gender)
{
@性别
}
@foreach (var gender in Model.Genders)
{
<input type="radio" asp-for="Gender" value="@gender" />@gender<br />
}