0

我正在尝试从控制器获取单选按钮列表并将其显示在视图上,但它似乎正在工作。我有以下型号

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。

我正在尝试的视图代码如下:

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

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

<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> 

}

上面的视图没有显示

谁能给我一个解决方案?

4

1 回答 1

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>
            }
于 2012-10-10T12:00:57.200 回答