asp.net mvc3 - 显示RadioButtons列表

时间:2012-10-10 11:47:17

标签: asp.net-mvc-3

我试图从控制器获取一个radiobutton列表并将其显示在View上,但它似乎正在工作。我有以下型号

public class Module
{

    public int ModuleId { get; set; }

    public string Title { get; set; }

    public string Description { get; set; }

    public bool IsActive { get; set; }

}

在控制器中,我获取模块列表并将其传递给视图:

[HttpGet]
    public ActionResult SelectAdmin()
    {
        //getting the list of modules from db
        List<Module> modulelst = GetModules(); 
        ViewBag.lstm = modulelst;

        return PartialView();
    }

现在在视图中我试图以RadioButtons / Checkboxes的形式显示模块列表,并获取所选模块的相应ID。

我正在尝试的View的代码如下:

@model List<MyFriday.Domain.Model.Client>

@using(Ajax.BeginForm(“SelectAdmin”,“CMAdmin”,null,         新的AjaxOptions         {             UpdateTargetId =“update-message”,             InsertionMode = InsertionMode.Replace,             HttpMethod =“POST”,             OnSuccess =“UpdateSuccess”         },new {@id =“EditForm”})) {      @ Html.ValidationSummary(真)

<fieldset>

<div class="content">                     
            @using (Html.BeginForm())
            {
                <div>
                Select the Admin Type:
                 </div>
                <div id="divModules">
                @foreach (var Modules in ViewBag.lstm)
                {
                    <div class="formlabel">
                        <div align="right" class="label" style="cursor: pointer; position: relative;">
                            @(Modules.First().Title)
                        </div>
                    </div>
                    <div class="formelement">
                        <input type="radio" id=""/>
                            @(Modules.First().ModuleId)

                    </div>
                    <div style="clear: both;">
                    </div>
                }
                </div>
                <input id="Submit1"  type="submit" value="Submit"/>
                <input type="button" value="Cancel" onclick="javascript:cancelconfirm(this)"/>   

            }
            <div style="clear: both;">
                &nbsp;</div>

        </div>

</fieldset> 

}

上方视图未显示

有人可以建议我一个解决方案吗?

1 个答案:

答案 0 :(得分:0)

首先我要改变控制器:

[HttpGet]
public ActionResult SelectAdmin()
{
    //getting the list of modules from db
    List<Module> modulelst = GetModules(); 
    // ViewBag.lstm = modulelst;

    return PartialView(modulelst);
}

然后更改视图...添加它在第一行使用模型的decalration:

@model List<Module>

然后将for循环更改为:

            @foreach (var module in Model)
            {
                <div class="formlabel">
                    <div align="right" class="label" style="cursor: pointer; position: relative;">
                        @Html.DisplayFor(modelItem => module.Title, true)

                    </div>
                </div>
                <div class="formelement">
                    <input type="radio" id=""/>
                        @RadioButtonFor(modelItem => module.ModuleId, new { @id="ModuleId"})

                </div>
                <div style="clear: both;">
                </div>
            }