1

我正在尝试在 Razor 中创建一个 Ajax Telerik 网格,该网格具有一个可更新的外键列,该列显示一个下拉列表。我已经像示例一样复制了我的页面,并且一切正常。我可以添加、删除和编辑新记录。唯一不起作用的是,当我更新网格中的记录时,我得到一个带有整数的文本字段,而不是一个包含外键表所有可能性的下拉列表。

有人对我如何解决这个问题有任何想法吗?请参阅下面的代码。

Telerik 网格:

@(Html.Telerik().Grid<EditableAccount>()
    .Name("Grid")
    .ToolBar(commands => commands.Insert().ButtonType(GridButtonType.Text).ImageHtmlAttributes(new { style = "margin-left:0" }))
    .DataBinding(dataBinding => dataBinding.Ajax()
        .Insert("InsertAccount", "Administration")
        .Update("SaveAccount", "Administration")
        .Delete("DeleteAccount", "Administration"))
    .DataKeys(keys => { keys.Add(a => a.AccountId); })
    .Columns(columns =>
    {
        columns.ForeignKey(b => b.BankId, (IEnumerable)ViewData["Banks"], "ID", "Name").Width(50);
        columns.Bound(a => a.AccountNumber).Width(110);
        columns.Command(commands =>
        {
            commands.Edit().ButtonType(GridButtonType.Image);
            commands.Delete().ButtonType(GridButtonType.Image);
        }).Width(16);
    })
    .Editable(editing => editing.Mode(GridEditMode.InLine))
    .Pageable()
    .Scrollable()
    .Sortable()
)

控制器:

[GridAction]
public ActionResult Accounts()
{
    ViewData["Banks"] = db.Banks.Select(b => new { Id = b.BankId, Name = b.Name });
    return View(new GridModel(accountRepository.All()));
}

[AcceptVerbs(HttpVerbs.Post)]
[GridAction]
public ActionResult InsertAccount()
{
    //Create a new instance of the EditableProduct class.
    EditableAccount account = new EditableAccount();

    //Perform model binding (fill the product properties and validate it).
    if (TryUpdateModel(account))
    {
        //The model is valid - insert the product.
        accountRepository.Insert(account);
    }

    //Rebind the grid
    return View(new GridModel(accountRepository.All()));
}

[AcceptVerbs(HttpVerbs.Post)]
[GridAction]
public ActionResult SaveAccount(int id, int bankId)
{
    EditableAccount account = new EditableAccount
    {
        AccountId = id,
        Bank = db.Banks
                   .Where(b => b.BankId == bankId)
                   .Select(b => b.Name).SingleOrDefault(),
        BankId = bankId
    };

    TryUpdateModel(account);

    accountRepository.Update(account);

    return View(new GridModel(accountRepository.All()));
}

[AcceptVerbs(HttpVerbs.Post)]
[GridAction]
public ActionResult DeleteAccount(int id)
{
    //Find a customer with ProductID equal to the id action parameter
    EditableAccount account = accountRepository.One(a => a.AccountId == id);

    if (account != null)
    {
        //Delete the record
        accountRepository.Delete(account);
    }

    //Rebind the grid
    return View(new GridModel(accountRepository.All()));
}

模型:

public class EditableAccount
{
    [ScaffoldColumn(false)]
    public int AccountId { get; set; }

    [Required]
    [UIHint("GridForeignKey")]
    [DisplayName("Bank")]
    public int BankId { get; set; }
    public string Bank { get; set; }

    [Required]
    [DisplayName("AccountNumber")]
    public int AccountNumber { get; set; }
}

存储库:

public IList<EditableAccount> All()
{
    IList<EditableAccount> result =
            (from account in db.Accounts
             select new EditableAccount
             {
                 AccountId = account.AccountId,
                 Bank = account.Bank.Name,
                 BankId = account.BankId,
                 AccountNumber = account.AccountNr
             }).ToList();

    return result;
}

public EditableAccount One(Func<EditableAccount, bool> predicate)
{
    return All().Where(predicate).FirstOrDefault();
}

public void Insert(EditableAccount insertedAccount)
{
    Account account = new Account();
    account.BankId = insertedAccount.BankId;
    account.AccountNr = insertedAccount.AccountNumber;
    db.Accounts.InsertOnSubmit(account);
    db.SubmitChanges();
}

public void Update(EditableAccount updatedAccount)
{
    Account account = db.Accounts.SingleOrDefault(a => a.AccountId == updatedAccount.AccountId);
    account.BankId = updatedAccount.BankId;
    account.AccountNr = updatedAccount.AccountNumber;
    db.SubmitChanges();
}

public void Delete(EditableAccount deletedAccount)
{
    Account account = db.Accounts.SingleOrDefault(a => a.AccountId == deletedAccount.AccountId);
    db.Accounts.DeleteOnSubmit(account);
    db.SubmitChanges();
}
4

1 回答 1

0

有人在 Telerik 论坛上回答了我的问题: http ://www.telerik.com/community/forums/aspnet-ajax/grid/asp-net-mvc-razor-grid-with-editable-foreign-key-dropdown-column .aspx

于 2012-06-05T10:02:53.280 回答