如何使用foreach循环创建ajax网格然后将数据绑定到其列?

时间:2016-01-26 15:14:48

标签: c# ajax gridview model-view-controller viewmodel

这是用

强列入的视图
@using HRHPMVS.ViewModel
@using HRHPMVS.Models
@model IEnumerable< HRHPMVS.ViewModel.NationalityVM>

查看模型EthnicVM:

public class NationalityVM
{
    public Nationality Nationality { get; set; }

这是模型:

public class Nationality
{
    public int ID { get; set; }
    [Display(Name = "Name")]
    [Required(ErrorMessage = "Name is Requirde")]
    [RegularExpression(@"^[a-zA-Z]+$", ErrorMessage = "please: Use letters only ")]
    public string Name { get; set; }
    [Display(Name = "Code")]
    [Required(ErrorMessage = "Code is Requirde")]
    [RegularExpression(@"[0-9]*\.?[0-9]+", ErrorMessage = "{0} must be a Number.")]
    [Range(0, int.MaxValue, ErrorMessage = "Please: enter valid integer Number")]
    public int Code { get; set; }
    public Nullable<short> IsActive { get; set; }

这是我的存储库功能:

public List <NationalityVM> list()
{
    List<NationalityVM> l1 = new List<NationalityVM>();
    SqlDataReader DR ;

    DR = Ado.ExecDataReaderProc("NationalitiesSelectAll");

    while (DR.Read())
    {
        NationalityVM vm = new NationalityVM();
        vm.Nationality = new Nationality
        {
            Code = Convert.ToInt32(DR["Code"]),
            IsActive = Convert.ToInt16(DR["IsActive"]),
            Name = Convert.ToString(DR["Name"]),
            ID = Convert.ToInt32(DR["ID"])
        };

        l1.Add(vm);

    }

    return l1;

这是我的对照:

public ActionResult list()
{
    List<NationalityVM> model = NationRepObj.list();
    ModelState.Clear();
    return View(model);
}

这是我的网格视图:

<div id="gridContent" style="padding-right: 40%;">
    @grid.GetHtml(
        tableStyle: "webGrid",
        fillEmptyRows: false,
        alternatingRowStyle: "alt",
        headerStyle: "grid-header",
        footerStyle: "foot-grid",
        mode: WebGridPagerModes.All,
        firstText: "<< First",
        previousText: " < Previous",
        nextText: "Next >",
        lastText: "Last >>",
        caption: "Nationalties Details",
        emptyRowCellValue: null,
        columns:
        grid.Columns(
            grid.Column("ID", format: @<text>
                <span class="display-mode">@Model.GetEnumerator()</span>
                <label id="ID" class="edit-mode"></label>
                </text>),
            grid.Column("Name", "Name", format: @<text>
                <span class="display-mode">
                <label id="lblName">@item.Name</label>
                </span>
                <input type="text" id="Name" value="" class="edit-mode" />
                </text>),
            grid.Column("Code", "Last Name", format: @<text>
                <span class="display-mode">
                <label id="lblCode"></label>
                </span>
                <input type="text" id="Code" value="" class="edit-mode" /></text>),
            grid.Column("IsActive", "IsActive", format: @<text>
                <span class="display-mode">
                <label id="lblIsActive"></label>
                </span>
                <input type="text" id="IsActive" value="" class="edit-mode" />
                </text>),
            grid.Column("Action", format: @<text>
                <button class="edit-user display-mode">  Edit</button>
                <button class="delete-user display-mode"> Delete</button>
                <button class="save-user edit-mode">  Save</button>
                <button class="cancel-user edit-mode"> Cancel</button>
                </text>, canSort: false)
        )
    )
</div>

我的模型不包含任何项目我必须使用foreach循环frist:

@if( Model !=null)
{
    foreach ( var item in Model)
    {
        @item.Nationality.Code
        @item.Nationality.ID
        @item.Nationality.Name
        @item.Nationality.IsActive

如何在网格中进行循环才能显示项目?

0 个答案:

没有答案