【问题标题】:Is it possible to redirect different pages by checking radio button value in form submit?是否可以通过检查表单提交中的单选按钮值来重定向不同的页面?
【发布时间】:2021-04-16 20:13:24
【问题描述】:
<!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>&nbsp;</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();
                }
            }
        }
    }
}

此代码来自我正在处理的网站的其中一个剃须刀页面。该代码当前设置为将用户发送到一个通用登录页面的位置。我想让用户点击一个单选按钮来区分病人或医生并将他们发送到相应的网页。我对 asp.net 还很陌生,所以我不是 100% 可以做到这一点。

【问题讨论】:

    标签: c# html asp.net


    【解决方案1】:

    您可以将Gender 属性添加到模型,以便您可以通过检查OnPost 中的值来重定向不同的页面,如下所示:

    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 (var gender in Model.Genders)
    {
        <input type="radio" asp-for="Gender" value="@gender" />@gender<br />
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-14
      • 2012-07-24
      • 2016-01-09
      • 1970-01-01
      • 1970-01-01
      • 2014-10-09
      • 2014-08-09
      • 2016-08-06
      相关资源
      最近更新 更多