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

Problem implementing drop down list as editor control for foreign key

6 Answers 174 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.
Stephan Kühn
Top achievements
Rank 1
Stephan Kühn asked on 28 Nov 2011, 10:10 AM
Hello,

I currently set up a Grid with GridEditMode.InLine to edit a child object which belongs to exactly one of a set of parent objects. I am using MVC 3 and Razor as view engine. I tried to replicate the approach from the respective demo application.

I am using the Visual Studio Default folder structure, i.e. my view resides in Views/childObject.
...
columns.ForeignKey(c => c.ClientId, (IEnumerable)ViewData["clients"], "Id", "Name").Title("Client");
...

My controller populates the View Data with the list of id and name named "clients".
[OutputCache(Duration = 0)]
[CultureAwareAction]
public ActionResult Index()
{
    PopulateClients();
    var repository = new MarketingAuthHolderRepository();
    return View(MarketingAuthHolderModel.GetAllModels(repository.GetAll()));
}
 
[GridAction]
[CultureAwareAction]
public ActionResult SelectAjaxEditing()
{
    PopulateClients();
    var repository = new MarketingAuthHolderRepository();
    return View(new GridModel(MarketingAuthHolderModel.GetAllModels(repository.GetAll())));
}
 
private void PopulateClients()
{
    IRepository<Client> repository = new ClientRepository();
    ViewData["clients"] = repository.GetAll()
                                .Select(e => new { Id = e.Id, Name = e.Name })
                                .OrderBy(e => e.Name);
}
 
[AcceptVerbs(HttpVerbs.Post)]
[GridAction]
[CultureAwareAction]
public ActionResult SaveAjaxEditing(int id)
{
    var repository = new MarketingAuthHolderRepository();
    var entity = repository.GetById(id);
    var model = new MarketingAuthHolderModel(entity);
    IRepository<Client> clientRepository = new ClientRepository();
    model.Client = clientRepository.GetAll()
        .Where(e => e.Id == model.ClientId)
        .Select(e => e.Name)
        .SingleOrDefault();
 
    if (TryUpdateModel(model, null, null, new[] { "Client" }))
    {
        model.UpdatedOn = DateTime.Now;
        model.UpdatedBy = User.Identity.Name;
        repository.Update(model.ToEntity());
    }
 
    return View(new GridModel(MarketingAuthHolderModel.GetAllModels(repository.GetAll())));
}


I have annotated my model to point at an editor template.
[Key]
public int Id { get; set; }
 
[Required]
[Display(Name = "Name")]
public string Name { get; set; }
 
[UIHint("ClientMarketingAuthHolder"), Required]
public string Client { get; set; }
 
public int? ClientId { get; set; }

I have set up the following Editor template ClientMarketingAuthHolder.cshtml:
@using System.Collections
@using Telerik.Web.Mvc.UI
@(Html.Telerik().DropDownList()
        .Name("Client")
        .BindTo(new SelectList((IEnumerable)ViewData["clients"], "Id", "Name"))
)

For display, this works just fine. However, as soon as I edit a column in my view, the display changes from the parent name to the parent id. I have tried placing my Editor template in various locations:
in ~/Views/MarketingAuthHolder/EditorTemplates
in ~/Views/MarketingAuthHolder
in ~/Views/Shared/EditorTemplates

None of the above work. What am I missing here?

Kind Regards
Stephan

6 Answers, 1 is accepted

Sort by
0
Stephan Kühn
Top achievements
Rank 1
answered on 28 Nov 2011, 01:39 PM
Hello, I found a solution. I had used 2011 Q2 Release to set up my project, and have recently upgrade to 2011 Q3 both my machine and the project using the upgrade wizard. It appears to me that a file GridForeignKey.cshtml in Views/Shared/EditorTemplates was not correctly copied when upgrading my project. That was the missing piece in order to make ForeignKey column work. Apparently, this is also missing in your example.

Please transfer it to your issue tracker as I am somehow not able to raise issues in that system!

Kind Regards
Stephan
0
Max
Top achievements
Rank 1
answered on 07 Dec 2011, 11:22 PM
very interesting! but I am using the latest Telegrid Grid version but can't seem to have the following method:columns.ForeignKey
how did you get that?

thanks
0
Max
Top achievements
Rank 1
answered on 08 Dec 2011, 05:45 PM
nevermind...found it! :)
0
steven
Top achievements
Rank 1
answered on 11 Dec 2011, 03:29 AM
Thanks Stephan, this helped me as well!
0
Yuriy
Top achievements
Rank 1
answered on 17 Jan 2012, 04:25 AM
Is the the way to set lookup(ForeignKey) field in Q2? We didn't upgrade to Q3 yet, but would love to have this functionality for Grid MVC, Razor view, Ajax editing. Thank you! -Yuriy
0
Stephan Kühn
Top achievements
Rank 1
answered on 19 Jan 2012, 09:28 AM
Hello, please take my answers with a grain of salt as I am just a Telerik user, not support staff. Afaik, ForeignKey() was a new feature introduced in Q3. I could easily upgrade my project from Q2 to Q3 after installing Q3 using the upgrade wizard. Be careful as you answer it's questions, as I ended up somehow missing the above mentioned GridForeignKey.cshtml in Views/Shared/EditorTemplates.

Kind Regards and good luck,
Stephan
Tags
Grid
Asked by
Stephan Kühn
Top achievements
Rank 1
Answers by
Stephan Kühn
Top achievements
Rank 1
Max
Top achievements
Rank 1
steven
Top achievements
Rank 1
Yuriy
Top achievements
Rank 1
Share this question
or