简体   繁体   English

使用“剃刀”视图中的单选按钮?

[英]Use radio button in the Razor view?

I want to use two radio buttons to check whether the user registration type is customer or business. 我想使用两个单选按钮来检查用户注册类型是客户还是公司。 So for that i have coded the View.cshtml file as in 因此,我已经将View.cshtml文件编码为
this image 这个图片

But I'm getting an unexpected output. 但是我得到了意外的输出。 It's not displaying the radio button. 它没有显示单选按钮。 showing only text instead 只显示文字

Here is my code: 这是我的代码:

    @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

You are already using the HtmlHelper, so you could just use the radio button method. 您已经在使用HtmlHelper,因此可以只使用单选按钮方法。

So instead of the <input> tags you could just display: 因此,您可以只显示<input>标记而不是:

Customer: @Html.RadioButtonFor(model => model.user_type, "Customer") 客户:@ Html.RadioButtonFor(model => model.user_type,“客户”)
Business: @Html.RadioButtonFor(model => model.user_type, "Business") 业务:@ Html.RadioButtonFor(model => model.user_type,“业务”)

There's more information about it here: 这里有更多关于它的信息:

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

All you need to do is use the HTML helper for Radio Buttons: 您需要做的就是为单选按钮使用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>

I hope this helps! 我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM