如何在MVC剃刀视图中设置在编辑模式下选中的单选按钮

时间:2014-08-08 05:31:00

标签: asp.net-mvc-4 razor

我是MVC剃须刀的新手。我正在尝试编辑网页。其中我有两个单选按钮。我成功保存单选按钮值的数据。但是当我尝试编辑该值时,我无法选择保存数据的单选按钮。 我有针对性别的枚举

public enum Gender
{
    Male,
    Female
}

我的创建代码是: -

<div class="editor-label">
   @Html.LabelFor(model => model.gender)
</div>
<div class="editor-field">
   @Html.RadioButtonFor(x => x.gender, (int)Gender.Male) Male
   @Html.RadioButtonFor(x => x.gender, (int)Gender.Female) Female
</div>

编辑代码就像

<div class="editor-label">
   @Html.LabelFor(model => model.gender)
</div>
<div>
   @if (Model.gender == (int)Gender.Male)
   {
   @Html.RadioButtonFor(model => model.gender, "Male", new { @checked = true })
   @Html.Label("Male")
   @Html.RadioButtonFor(model => model.gender, "Female")
   @Html.Label("Female")
   }
   else
   {
   @Html.RadioButtonFor(model => model.gender, "Male")
   @Html.Label("Male")
   @Html.RadioButtonFor(model => model.gender, "Female", new { @checked = true })
   @Html.Label("Female")
   }
</div>

6 个答案:

答案 0 :(得分:32)

你写的像

@Html.RadioButtonFor(model => model.gender, "Male", new { @checked = true }) and
@Html.RadioButtonFor(model => model.gender, "Female", new { @checked = true })

此处您已将性别视为Enum类型,并且您已将单选按钮的值写为string类型 - 更改&#34;男性&#34;到0和&#34;女&#34;到1。

答案 1 :(得分:11)

请勿在视图级别执行此操作。只需在视图模型的构造函数中将默认值设置为该属性即可。干净简单。在您的回发中,您选择的值将自动填充正确的选择。

例如

public class MyViewModel
{
        public MyViewModel()
        {
            Gender = "Male";
        }
}

<table>
  <tr>
	<td><label>@Html.RadioButtonFor(i => i.Gender, "Male")Male</label></td>
	<td><label>@Html.RadioButtonFor(i => i.Gender, "Female")Female</label></td>
  </tr>
 </table>

答案 2 :(得分:2)

要在MVC剃刀视图中设置在编辑模式下选中的单选按钮,请尝试以下操作:

<div class="col-lg-11 col-md-11">
     @Html.RadioButtonFor(m => m.Role, "1", Model.Role == 1 ? "checked" : string.Empty) 
     <span>Admin</span>
     @Html.RadioButtonFor(m => m.Role, "0", Model.Role == 0 ? "checked" : string.Empty)
     <span>User</span>
     @Html.HiddenFor(m => m.Role)
</div>

希望有帮助!

答案 3 :(得分:0)

以下是我的工作方式,适用于创建和编辑:

//How to do it with enums
<div class="editor-field">
   @Html.RadioButtonFor(x => x.gender, (int)Gender.Male) Male
   @Html.RadioButtonFor(x => x.gender, (int)Gender.Female) Female
</div>

//And with Booleans
<div class="editor-field">
   @Html.RadioButtonFor(x => x.IsMale, true) Male
   @Html.RadioButtonFor(x => x.IsMale, false) Female
</div>

提供的值(true和false)是引擎将作为html元素的值呈现的值,即:。

<input id="IsMale" type="radio" name="IsMale" value="True">
<input id="IsMale" type="radio" name="IsMale" value="False">

checked属性取决于Model.IsMale值。

Razor引擎似乎在内部将设置的单选按钮值与模型值匹配,如果存在适当的from和to string convert。 因此,无需在辅助方法中将其添加为html属性。

答案 4 :(得分:0)

添加选中了两个单选按钮。然后在文档上显示/隐藏所需的内容。

 <div class="form-group">
                            <div class="mt-radio-inline" style="padding-left:15px;">
                                <label class="mt-radio mt-radio-outline">
                                    Full Edition
                                    <input type="radio" value="@((int)SelectEditionTypeEnum.FullEdition)" asp-for="SelectEditionType" checked>
                                    <span></span>
                                </label>
                                <label class="mt-radio mt-radio-outline">
                                    Select Modules
                                    <input type="radio" value="@((int)SelectEditionTypeEnum.CustomEdition)" asp-for="SelectEditionType" checked>
                                    <span></span>
                                </label>

                            </div>
                        </div>

答案 5 :(得分:0)

这是获取选中单选按钮的值并在编辑表单中根据其值设置选中的单选按钮的代码:

控制器:

[HttpPost]
public ActionResult Create(FormCollection collection)
{
    try
    {
        CommonServiceReference.tbl_user user = new CommonServiceReference.tbl_user();
        user.user_gender = collection["rdbtnGender"];
        return RedirectToAction("Index");
    }
    catch(Exception e)
    {
        throw e;
    }
}

public ActionResult Edit(int id)
{
    CommonServiceReference.ViewUserGroup user = clientObj.getUserById(id);
    ViewBag.UserObj = user;
    return View();
}

查看:

创建:

<input type="radio" id="rdbtnGender1" name="rdbtnGender" value="Male" required>
<label for="rdbtnGender1">MALE</label>
<input type="radio" id="rdbtnGender2" name="rdbtnGender" value="Female" required>
<label  for="rdbtnGender2">FEMALE</label>

编辑:

<input type="radio" id="rdbtnGender1" name="rdbtnGender" value="Male" @(ViewBag.UserObj.user_gender == "Male" ? "checked='true'" : "") required>
<label for="rdbtnGender1">MALE</label>

<input type="radio" id="rdbtnGender2" name="rdbtnGender" value="Female" @(ViewBag.UserObj.user_gender == "Female" ? "checked='true'" : "") required>
<label for="rdbtnGender2">FEMALE</label>