在Razor视图中使用单选按钮?

时间:2016-10-12 16:12:04

标签: c# asp.net-mvc entity-framework

我想使用两个单选按钮来检查用户注册类型是客户还是业务。因此,我已经编写了View.cshtml文件,如同 this image

但是我得到了意想不到的输出。它没有显示单选按钮。showing only text instead

这是我的代码:

    @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

2 个答案:

答案 0 :(得分:2)

您已经在使用HtmlHelper,因此您可以使用单选按钮方法。

因此,您可以只显示<input>标记而不是

客户:@ Html.RadioButtonFor(model =&gt; model.user_type,“客户”)
业务:@ Html.RadioButtonFor(model =&gt; model.user_type,“Business”)

这里有更多相关信息:

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

答案 1 :(得分:0)

您需要做的就是使用Radio Heltons的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>

我希望这有帮助!