在Razor视图中使用单选按钮

本文关键字:单选按钮 Razor 视图 | 更新日期: 2023-09-27 18:12:50

我想使用两个单选按钮来检查用户注册类型是客户还是企业。因此,我已经编码了视图。CSHTML文件,如
这张图片

但是我得到了一个意想不到的输出。它没有显示单选按钮。只显示文本而不是

下面是我的代码:
    @model FoodieWeb.Models.UsersModel
@{
    ViewBag.Title = "UserRegister";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@using (Html.BeginForm("UserRegister", "UserRegister", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) 
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        <h4>Register as a new Member</h4>
        <hr />
        @Html.ValidationSummary("", new { @class = "text-danger" })
        <div class="form-group">
            <label for="email" class="col-md-2 control-label">Email*</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.email, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.email)
            </div>
        </div>
        <div class="form-group">
            <label for="fname" class="col-md-2 control-label">First Name*</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.fname)
                @Html.ValidationMessageFor(model => model.fname)
            </div>
        </div>
        <div class="form-group">
            <label for="lname" class="col-md-2 control-label">Last Name*</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.lname)
                @Html.ValidationMessageFor(model => model.lname)
            </div>
        </div>
        <div class="form-group">
            <label for="phone" class="col-md-2 control-label">Phone*</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.phone)
                @Html.ValidationMessageFor(model => model.phone)
            </div>
        </div>
        @*<div class="form-group">
                @Html.LabelFor(model => model.photo, new { @class = "col-md-2 control-label" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.photo)
                    @Html.ValidationMessageFor(model => model.photo)
                </div>
            </div>*@
        <div class="form-group">
            <label for="address" class="col-md-2 control-label">Address*</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.address)
                @Html.ValidationMessageFor(model => model.address)
            </div>
        </div>
        <div class="form-group">
            <label for="city" class="col-md-2 control-label">City*</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.city)
                @Html.ValidationMessageFor(model => model.city)
            </div>
        </div>
        <div class="form-group">
            <label for="postcode" class="col-md-2 control-label">Post Code*</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.postcode)
                @Html.ValidationMessageFor(model => model.postcode)
            </div>
        </div>
        <div class="form-group">
            <label for="district" class="col-md-2 control-label">District*</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.district)
                @Html.ValidationMessageFor(model => model.district)
            </div>
        </div>
        <div class="form-group">
            <label for="user_type" class="col-md-2 control-label">User Type*</label>
            <div class="col-md-10">
                @Html.RadioButtonFor(model => model.user_type, "Customer")
                @Html.RadioButtonFor(model => model.user_type, "Business")
                @*@Html.EditorFor(model => model.user_type)*@
                @Html.ValidationMessageFor(model => model.user_type)
            </div>
        </div>
        <div class="form-group">
            <label for="status" class="col-md-2 control-label">Status*</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.status)
                @Html.ValidationMessageFor(model => model.status)
            </div>
        </div>
        <div class="form-group">
            <label for="created_date" class="col-md-2 control-label">Created Date*</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.created_date)
                @Html.ValidationMessageFor(model => model.created_date)
            </div>
        </div>
        <div class="form-group">
            <label for="password" class="col-md-2 control-label">Password*</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.password)
                @Html.ValidationMessageFor(model => model.password)
            </div>
        </div>
        <div class="form-group">
            <label for="confirm_password" class="col-md-2 control-label">Confirm Password*</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.confirm_password)
                @Html.ValidationMessageFor(model => model.confirm_password)
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-10">
                <label>
                    <input id="agree" type="checkbox" name="agree" checked value="true" />
                    @*@Html.CheckBox("agree", true)*@
                    @*@Html.CheckBoxFor(model => model.agree)I agree to the terms and conditions.
                    @Html.ValidationMessageFor(model => model.agree)*@
                </label>
            </div>
        </div>
        <div class="form-group" style="visibility:visible">
            <div class="col-md-offset-2 col-md-10" style="visibility:visible">
                <input type="submit" class="btn btn-primary" />
            </div>
        </div>
    </div>
}
<div>
    @*@Html.ActionLink("Back to List", "Index")*@
    @Html.ActionLink("Already Registered?", "UserLogin", "UserLogin")
</div>
    enter code here

在Razor视图中使用单选按钮

你已经使用了HtmlHelper,所以你可以只使用单选按钮方法。

代替<input>标签你可以直接显示:

顾客:@Html。RadioButtonFor(model => model。user_type,"客户")
业务:@Html。RadioButtonFor(model => model。user_type,"业务")

这里有更多关于它的信息:

http://www.tutorialsteacher.com/mvc/htmlhelper-radiobutton-radiobuttonfor

你所需要做的就是使用单选按钮的HTML帮助器:

<div class="form-group">
    @Html.LabelFor(model => model.user_type, "Customer:", htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.RadioButtonFor(model => model.user_type, "Customer")
        @Html.ValidationFor(model => model.user_type, new { @class = "text-danger" })
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(model => model.user_type, "Business:", htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.RadioButtonFor(model => model.user_type, "Business")
        @Html.ValidationFor(model => model.user_type, new { @class = "text-danger" })
    </div>
</div>

我希望这对你有帮助!