【问题标题】:How to display .cshtml content based on ActionLink in .NET MVC?如何在 .NET MVC 中基于 ActionLink 显示 .cshtml 内容?
【发布时间】:2020-06-01 07:16:44
【问题描述】:

我的 Register.cshtml 页面包含患者和医生注册。我编写了一个脚本来检查用户是否是患者,然后仅显示与患者相关的信息,反之亦然。 如果点击注册患者,则只需将患者信息插入数据库,反之亦然用于医生注册。

目前,如果我注册患者,则不应将专业数据输入数据库,因为它是医生的特定信息。

另一个问题是,如果我从下拉列表中更改专业或城市值,则会引发错误:

具有键 'Specialization' 的 ViewData 项属于类型 'System.String' 但必须是 'IEnumerable' 类型。

异常详细信息:System.InvalidOperationException:ViewData 项 具有键 'Specialization' 的类型为 'System.String' 但必须 属于“IEnumerable”类型。

源错误:@Html.DropDownListFor(x => Model.Specialization, Model.Specializations, htmlAttributes: new { @class= "form-control form-control-sm" })

Register.cshtml

model Aayumitra.Models.RegisterViewModel

<!-- Register Form -->
<div id="accounts-form">
    <div class="account-nav">
        <div class="row">
            <div class="col-md-12 text-center">
                <ul class="d-flex align-items-center justify-content-center">

                    <li>@Html.ActionLink("Login", "Login", "Account", null, new { @class = "mr-4" })</li>
                    <li>@Html.ActionLink("Register", "Register", "Account", null, new { @class = "active" })</li>


                </ul>
            </div>
        </div>
    </div>

    <script>
        function getUrlVars() {
            var vars = {};
            var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function (m, key, value) {
                vars[key] = value;
            });
            return vars;
        }

        var text = getUrlVars()["profile"];

        let profile;
        if (text) {
            if (text == 'doctor') {
                profile = 'doctor';
            }
            if (text == 'patient') {
                profile = 'patient';
            }
        } else {
            profile = 'patient';
        }
        $(document).ready(function () {
            if (profile === "doctor") {
                $(".doctor").show();
            }
            else {
                $(".doctor").hide();
            }
        });
    </script>

    <div class="container">
        <div class="row py-5 border-top">
            <div class="col-md-6">
                <div class="form-img text-right">
                    <img src="~/Content/images/accounts.png" class="img-fluid" alt="Accounts Image">
                </div>
            </div>
            <div class="col-md-5">
                <div class="card">
                    <div id="form-card-header" class="card-header d-flex justify-content-between bg-white">
                        <h6>Join Aayumitra </h6>
                        <span class="float-right">Are you a Doctor? <span>@Html.ActionLink("register here", "register", "Account", new { profile = "doctor" }, null)</span>

                    </div>

                    <script>
    var formCardHeader = document.getElementById('form-card-header');

    if(profile == 'patient'){
        formCardHeader.innerHTML = `
        <h6>Join Aayumitra </h6>
                            <span class="float-right">Are you a Doctor? <span>@Html.ActionLink("register here", "register", "Account", new { profile="doctor"}, null)</</span>
        `
    }

    if (profile == 'doctor'){
        formCardHeader.innerHTML = `
        <h6 id="join-doctors">Join 1000+ doctors</h6>
                            <span id="not-doctor" class=""text-primary>@Html.ActionLink("Not a doctor?", "register", "Account", new { profile="patient"}, null)</span>
         `
                        }

                    </script>

                    @using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
                    {

                        <div class="card-body">
                            <form action="register.chtml?profile=doctor">
                                <div class="form-group">
                                    <label for="FullName">First Name</label>
                                    @Html.TextBoxFor(m => m.FirstName, new { @class = "form-control form-control-sm", placeholder = "First Name" })
                                </div>
                                <div class="form-group">
                                    <label for="FullName">Last Name</label>
                                    @Html.TextBoxFor(m => m.LastName, new { @class = "form-control form-control-sm", placeholder = "First Name" })
                                </div>
                                <div class="form-group">
                                    <label for="email">Email</label>
                                    @Html.TextBoxFor(m => m.Email, new { @class = "form-control form-control-sm", placeholder = "Email ID" })

                                </div>                                

                                <div class="form-group doctor">
                                    <label for="number">Specialization</label>
                                    @Html.DropDownListFor(x => Model.Specialization, Model.Specializations, htmlAttributes: new { @class = "form-control form-control-sm", placeholder = "Specialization" })
                                </div>


                                <div class="form-group">
                                    <label for="number">City</label>
                                    @Html.DropDownListFor(x => Model.City, Model.Cities, htmlAttributes: new { @class = "form-control form-control-sm", placeholder = "City" })
                                </div>
                                <div class="form-group">
                                    <label for="number">Mobile Number</label>                                   

                                        <div class="col-md-8">
                                            <div class="form-group">
                                                @Html.TextBoxFor(m => m.MobileNumber, new { @class = "form-control form-control-sm", placeholder = "Mobile Number" })
                                            </div>
                                        </div>                                

                                </div>
                                <div class="form-group">
                                    <label for="password">Create Password</label>
                                    @Html.PasswordFor(m => m.Password, new { @class = "form-control form-control-sm", placeholder = "Password" })

                                </div>                                             

                                <div class="form-group">
                                    <input type="submit" value="Send" class="btn btn-primary btn-block">
                                </div>

                               </form>

                        </div>

                    }

                </div>
            </div>
        </div>
    </div>
</div>
<!-- Register Form End -->

控制器

// GET: /Account/Register
    [AllowAnonymous]
    public ActionResult Register()
    {
        var model = new RegisterViewModel();
        List<SelectListItem> SpecialitiesList = GetSpecialities();
        List<SelectListItem> CitiesList = GetCities();
        model.Specializations = SpecialitiesList;
        model.Cities = GetCities();
        return View(model);
    }

    //
    // POST: /Account/Register
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {                
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email,FirstName=model.FirstName,LastName=model.LastName,City=model.City, IsDoctor = model.IsDoctor, NMC_Number = model.NMC_Number, Specialization = model.Specialization};

            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
               return RedirectToAction("Index", "Home");
            }
            AddErrors(result);
        }         
        return View(model);
    }

【问题讨论】:

    标签: c# asp.net-mvc


    【解决方案1】:

    我认为解决它的最好方法是写两个注册动作(医生,病人),医生和病人有自己的看法

    【讨论】:

    • 能否请您根据情况提供解决方案。我不确定我将如何使用 2 注册操作方法。
    猜你喜欢
    • 1970-01-01
    • 2016-08-05
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 2019-12-31
    • 1970-01-01
    相关资源
    最近更新 更多