Thari Thari - 1 month ago 9
C# Question

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

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

Answer

You are already using the HtmlHelper, so you could just use the radio button method.

So instead of the <input> tags you could just display:

Customer: @Html.RadioButtonFor(model => model.user_type, "Customer")
Business: @Html.RadioButtonFor(model => model.user_type, "Business")

There's more information about it here:

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