This is a migrated thread and some comments may be shown as answers.

[Solved] Editing a column with a DropDownList

0 Answers 125 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Maria
Top achievements
Rank 1
Maria asked on 03 Jan 2011, 07:25 PM
Hi All!!!
I'm experienced the following problem working with a Grid. I have two table related, Category and Products. To show the Grid and do the edit I'm using a view create in SQL to show the columns from table Products and the Category name from the Category table.
The code of the view is :
<%@ Page Title="" Language="C#" UICulture="es-ES" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<ModeloDatos.Entidades.ArticuloDrop>>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Grid de Artículos
    <h3>
       Articulo ID
    </h3>
</asp:Content>
  
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
  
<h2>Grid de Artículos</h2>
<% Html.Telerik().Grid(Model)
        //.Grid<ModeloDatos.Entidades.ArticuloDrop>()
        .Name("GridArticulos")
        .Localizable("es-ES")
          
        // añadir toolBar Insert
        .ToolBar(commands => commands.Insert().ButtonType(GridButtonType.Text))
        .DataKeys(keys => keys.Add(e => e.ArticuloId ))
        .DataBinding(dataBinding => dataBinding.Ajax()
            .Select("Select", "ArticuloDrop")
            .Insert("Insert", "ArticuloDrop")
            .Update("Save", "ArticuloDrop")
            .Delete("Delete", "ArticuloDrop")) 
        .Columns(columnas =>
            {
                columnas.Bound(c => c.Nombre).Width(80);
                columnas.Bound(c => c.PrecioEuros).Width(50).Format(" {0:N2}").Title("Precio €");
                columnas.Bound(c => c.Categoria).Width(50);
                // añadir botones
                columnas.Command(command =>
                    {
                        command.Edit().ButtonType(GridButtonType.Text);
                        command.Delete().ButtonType(GridButtonType.Text);
                    }).Width(100).Title("Acciones");
            })
        .Sortable(sorting => sorting.SortMode(GridSortMode.SingleColumn))
        .Editable(editing => editing.Mode(GridEditMode.InLine))
        .Pageable(paging => paging.PageSize(3))
        .Filterable()
        .Render();
          
%>
<% Html.Telerik().ScriptRegistrar().Globalization(true); %>
<style type="text/css">
 
the model i'm using is :
public class ArticuloDrop
  {
      [ScaffoldColumn(false)]
      public int ArticuloId
      {
          get;
          set;
      }
      [Required]
      [DisplayName("Nombre del Artículo")]
      public string Nombre
      {
          get;
          set;
      }
      [DisplayName("Precio en Euros")]
      [DataType(DataType.Currency)]
      public decimal PrecioEuros
      {
          get;
          set;
      }
      public int CategoriaId
      {
          get;
          set;
      }
      [UIHint("CategoriaDropDown")]
      public string Categoria
      {
          get;
          set;
      }
  }

And the partial view to edit the dropdown is:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<% ViewData.TemplateInfo.HtmlFieldPrefix = string.Empty;
   Dictionary<string, string> types = new Dictionary<string, string>();
   ModeloDatos.ClarionWeb2010Entities entity = new ModeloDatos.ClarionWeb2010Entities();
   types.Add(string.Empty, "---Seleccione Opción ---");
   var datos = (from c in entity.Categorias
                             select new  { Id = c.CategoriaId, Name =c.Nombre }).ToList();
   foreach( var item in datos)
   {
       types.Add(item.Id.ToString(),item.Name); 
   }
%>
<%=Html.DropDownList("CategoriaId", new SelectList(types, "key", "value"))%>

This work perfect, when I'm editing a row which Category Id is "2" and  Category Name is "Category 2", the DropDownList show the name correct.
The problem I have is when instead of using the view created in SQL I use the table, always the DropDownList show the frist element.
Any idea about why ....

Thanks

Maria
Tags
Grid
Asked by
Maria
Top achievements
Rank 1
Share this question
or