得到名字类Razor?

时间:2015-05-18 17:39:56

标签: asp.net-mvc asp.net-mvc-4 razor view helper

public class ViewModel_Index
{
    public int Id { get; set; }
    public string Name { get; set; }
    [UIHint("_Date")]
    public DateTime bd { get; set; }
    [UIHint("_EnumRadioButton")]
    public Gender sex{ get; set; }
}

文件_EnumRadioButton.cshtml

@model Enum
@{
    List<SelectListItem> LstT = new List<SelectListItem>();
    Array ev = Enum.GetValues(Model.GetType());
    foreach (Enum item in ev)
    {
        LstT.Add(new SelectListItem
        {
            Text = Sample.Service.CustomExtension.EnumExtension.ToDescription(item),
            Value = item.ToString()
        });
    }
    @Helpers.RadioButtonList(Model.GetType().Name , LstT)
}

文件Helpers.cshtml

@helper RadioButtonList(string groupName, IEnumerable<System.Web.Mvc.SelectListItem> items)
{
    <div class="RadioButtonList">
        @foreach (var item in items)
        {
            @item.Text
            <input type="radio" name="@groupName" value="@item.Value"
            @if (item.Selected) { <text> checked="checked" </text>    } />
        }
    </div>
}

索引

@using (@Html.BeginForm("test", "Home", FormMethod.Post))
{
    @Html.EditorForModel()
    <button type="submit">send</button>
}

控制器

[HttpPost]
public ActionResult test(ViewModel_Index a)
{ 
    return View();
}

查看来源

<div class="RadioButtonList">
    زن<input type="radio" name="Gender" value="Woman"/>
    مرد<input type="radio" name="Gender" value="Man"/>
    هیچکدام<input type="radio" name="Gender" value="Non"/>
</div>

文件_EnumRadioButton.cshtml prablem: 名称无线电必须是同名字段

电台姓名=&#34;性别&#34; 字段名称=&#34;性别&#34;

@Helpers.RadioButtonList( Model.GetType()。Name ,LstT)

0 个答案:

没有答案