Telerik Forums
UI for ASP.NET AJAX Forum
9 answers
1.2K+ views
Hi all,

I am trying to create an Ajax Telerik grid in Razor that has an updateable foreign key column that shows a dropdown list. I've copied my page pretty much like the example, and everything works. I can add new records, delete them and edit them. The only thing that doesn't work is that I get a textfield with the integer when I update a record in my grid, instead of a dropdown list with all the possibilities of the foreign key table.

Anyone have any ideas on how I could fix this? See code below.

Telerik grid:
@(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()
)

Controller:
[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()));
}

Model:
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; }
}


Repository:

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;
}
 
/// <summary>
/// Ones the specified predicate.
/// </summary>
/// <param name="predicate">The predicate.</param>
/// <returns></returns>
public EditableAccount One(Func<EditableAccount, bool> predicate)
{
    return All().Where(predicate).FirstOrDefault();
}
 
/// <summary>
/// Inserts the specified account.
/// </summary>
/// <param name="account">The account.</param>
public void Insert(EditableAccount insertedAccount)
{
    Account account = new Account();
    account.BankId = insertedAccount.BankId;
    account.AccountNr = insertedAccount.AccountNumber;
    db.Accounts.InsertOnSubmit(account);
    db.SubmitChanges();
}
 
/// <summary>
/// Updates the specified account.
/// </summary>
/// <param name="account">The account.</param>
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();
}
 
/// <summary>
/// Deletes the specified account.
/// </summary>
/// <param name="account">The account.</param>
public void Delete(EditableAccount deletedAccount)
{
    Account account = db.Accounts.SingleOrDefault(a => a.AccountId == deletedAccount.AccountId);
    db.Accounts.DeleteOnSubmit(account);
    db.SubmitChanges();
}
Raja Patukuri
Top achievements
Rank 1
 answered on 02 Sep 2020
28 answers
790 views
Hello, I just updated to the Q3 release and I now can't seem to save my edited image with the RadImageEditor. After I crop an image and use the SaveEditableImage (server side) I can only save the original image. I have also tried getting the editable image first using GetEditableImage() but it too returns the original image.

Thanks,
Vessy
Telerik team
 answered on 02 Sep 2020
1 answer
1.5K+ views

Hello, I have a radgrid that is associated with a really large table as far as columns.  This table may have 60 columns in which data from different reports are written to this table.  I want to have a drop down that when a value is selected from the dropdown, I can set certain columns to be visible and certain columns to be hidden based on that selected value.  For example report 1 might use columns 1-15, and report 2 might use columns 1-5 and 16-25, so I want to be able to dynamically show and hide columns based on the column name that I plan to store in another table in relation to the report.  So when a user selects a report from a dropdown list, I can query the table that has what columns should be visible for the selected report, and dynamically show/hide columns in the radgrid based on that selection.

 

I am not finding anything in the documentation for this.  Any idea/suggestions would be appreciated.  Thanks.

Attila Antal
Telerik team
 answered on 02 Sep 2020
6 answers
763 views

I tried to change the ConfirmText and ConfirmTitle for a GridButtonColumn from the back-end in the ItemDataBound event, but this only applies after a PostBack. So if on the first load of the page, I clicked the Delete button, it would delete without a confirmation window. However, after any PostBack, the confirmation window comes up successfully. Am I using the wrong event to change this?

 

 

01.protected void rgPackagesHistory_ItemDataBound(object sender, GridItemEventArgs e)
02.        {
03.            if (e.Item is GridDataItem)
04.            {
05.                ((GridButtonColumn)rgPackagesHistory.MasterTableView.GetColumnSafe("column1abc")).ConfirmTitle = rxM.GetString("DeleteConfirmationTitle", cul);
06.                ((GridButtonColumn)rgPackagesHistory.MasterTableView.GetColumnSafe("column1abc")).ConfirmText = rxM.GetString("DeleteConfirmation", cul);
07.            }
08.        }

 

Peter Milchev
Telerik team
 answered on 02 Sep 2020
8 answers
352 views

Hi,
   I have master page , in that i added default.aspx, when click button in master page , i bind data in child page (default.aspx), Data are binding correctly, but how to update controls only in default.aspx, instead of refreshing full page and binding data. Master page contains ContentPlaceHolder1 which show defaullt.aspx page.



Thanks

M Kumar
Top achievements
Rank 1
Iron
Iron
Veteran
 answered on 02 Sep 2020
7 answers
477 views
Using a RadGrid for Winforms,
I am trying to enlarge the actual icons in the header of the grid. For example "pdf", "csv", and "Xls" . Is there a PNG somewhere that I can do this, or a style tag that can go in the aspx page?

Any help is appreciated.
Vessy
Telerik team
 answered on 01 Sep 2020
5 answers
153 views

I have a main page opening a radwindow with URL pointing to another domain. Now is there a way to track the rad window closing in the opening domain?

Main Page: https://www.sitename.com/subdomain1/mainpage.aspx

Radwindow URL: https://www.sitename.com/subdomain2/windowpage.aspx

When the Radwindow is closed i am able to trigger an event in "subdomain1/mainpage.aspx". But i would like to trigger an event (server side or client side) in "subdomain2/windowpage.aspx".

Peter Milchev
Telerik team
 answered on 01 Sep 2020
4 answers
206 views

Using the menu in the code below, my expectation is that the child menu items would expand immediately, but would collapse 5 seconds after the mouse leaves the item.  What's happening is the collapse happens immediately after the mouse leaves the item.  What am I not understanding about CollapseDelay.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="Test" %>
 
<!DOCTYPE html>
 
<head runat="server">
    <title></title>
    <style type="text/css">
         
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManagerProxy1" runat="server" />
 
        <telerik:RadSkinManager ID="RadSkinManager1" runat="server" Skin="Default" ShowChooser="false" />
 
        <telerik:RadFormDecorator ID="RadFormDecorator1" Runat="server" DecoratedControls="All" />
 
        <telerik:RadMenu ID="RadMenu1" runat="server"
            EnableRootItemScroll="true"
            EnableRoundedCorners="true" 
            EnableShadows="true"
            BorderWidth="0"
            Width="100%"
            CollapseDelay="5000"
        >
            <Items>
                <telerik:RadMenuItem Text="Guitars">
                    <Items>
                        <telerik:RadMenuItem Text="Classical Guitars">
                            <Items>
                                <telerik:RadMenuItem Text="1/2 Size Classical Guitars" />
                                <telerik:RadMenuItem Text="3/4 Size Classical Guitars" />
                                <telerik:RadMenuItem Text="7/8 Size Classical Guitars" />
                                <telerik:RadMenuItem Text="4/4 Size Classical Guitars" />
                                <telerik:RadMenuItem Text="Lefthanded Classical Guitars" />
                                <telerik:RadMenuItem Text="Concert Guitars" />
                            </Items>
                        </telerik:RadMenuItem>
                        <telerik:RadMenuItem Text="Steel String Guitars">
                            <Items>
                                <telerik:RadMenuItem Text="3/4 Size and Travel Guitars" />
                                <telerik:RadMenuItem Text="Dreadnought Guitars" />
                                <telerik:RadMenuItem Text="Jumbo Guitars" />
                                <telerik:RadMenuItem Text="Folk Guitars" />
                                <telerik:RadMenuItem Text="12-String Westernguitars" />
                                <telerik:RadMenuItem Text="Lefthanded Acoustic Guitars" />
                            </Items>
                        </telerik:RadMenuItem>
                        <telerik:RadMenuItem Text="Acoustic Basses" />
                        <telerik:RadMenuItem Text="Electric Guitars">
                            <Items>
                                <telerik:RadMenuItem Text="ST Models" />
                                <telerik:RadMenuItem Text="T-Models" />
                                <telerik:RadMenuItem Text="LP-Models" />
                                <telerik:RadMenuItem Text="SG Models" />
                                <telerik:RadMenuItem Text="7-String Guitars" />
                                <telerik:RadMenuItem Text="8-String Guitars" />
                                <telerik:RadMenuItem Text="Lefthanded Guitars" />
                            </Items>
                        </telerik:RadMenuItem>
                        <telerik:RadMenuItem Text="Electric Basses">
                            <Items>
                                <telerik:RadMenuItem Text="4-String J-Basses" />
                                <telerik:RadMenuItem Text="5-String J-Basses" />
                                <telerik:RadMenuItem Text="4-String P-Basses" />
                                <telerik:RadMenuItem Text="Misc. 4-String Basses" />
                                <telerik:RadMenuItem Text="Misc. 5-String Basses" />
                            </Items>
                        </telerik:RadMenuItem>
                        <telerik:RadMenuItem Text="Amplifiers for Acoustic Guitars" />
                        <telerik:RadMenuItem Text="Amplifiers for Electric Guitars" />
                        <telerik:RadMenuItem Text="Bass Amplifiers" />
                    </Items>
                </telerik:RadMenuItem>
            </Items>
        </telerik:RadMenu>
    </form>
</body>
</html>

 

Thanks,

Dave

 

Dave
Top achievements
Rank 1
Iron
Veteran
Iron
 answered on 31 Aug 2020
1 answer
120 views

Can someone help - I can't find anything in the docs, but it shoud be easy...

I have an image gallery working with sqldatasource and all looks good except the readability of the title text which appears over the image.

The customers for this website are 'senior citizens'  and can't read the captions. (see bessacarr-owners-club.org/photo-gallery.aspx)

My problem is that the text is a shade of grey and the surrounding box is also a (lighter) shade of grey. How can I make the text more contrasting.

eg the background darker and the text lighter?

Thanks

Clive

 

Q2 2016 controls in use

Vessy
Telerik team
 answered on 31 Aug 2020
7 answers
2.0K+ views

Hello -- I am not having any luck with opening a popup.  My code is listed below. 

Can someone please help?  I get an error - 'Unable to get property "open" of undefined or null reference

Thanks, Raka.

 

<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
    <script type="text/javascript">
        function editInfo() {
            if (confirm('Are you sure you want to do this?'))
                return window.radopen('/Folder/Edit.aspx', 'rwGeneric');
    }
    </script>
</telerik:RadCodeBlock>

Vessy
Telerik team
 answered on 31 Aug 2020
Narrow your results
Selected tags
Tags
+? more
Top users last month
Rob
Top achievements
Rank 3
Bronze
Bronze
Iron
Sergii
Top achievements
Rank 1
Iron
Iron
Dedalus
Top achievements
Rank 1
Iron
Iron
Lan
Top achievements
Rank 1
Iron
Doug
Top achievements
Rank 1
Want to show your ninja superpower to fellow developers?
Top users last month
Rob
Top achievements
Rank 3
Bronze
Bronze
Iron
Sergii
Top achievements
Rank 1
Iron
Iron
Dedalus
Top achievements
Rank 1
Iron
Iron
Lan
Top achievements
Rank 1
Iron
Doug
Top achievements
Rank 1
Want to show your ninja superpower to fellow developers?
Want to show your ninja superpower to fellow developers?