Lookup: key and name issue

Dec 11, 2010 at 8:48 AM

Hi, first of all thanks for this great library! After the library has been updated I'm experiencing some issues with the lookup component, probably it's just me or something has changed in the usage of the library, could you give a hint? In my scenario I'm using a simple lookup: my model has among other properties one defined like:

[LocalizableDisplayName(typeof(Resources), "PrimaryTeacher")]
public string PrimaryTeacher { get; set; }
[Required(ErrorMessageResourceName = "PrimaryTeacherRequired", ErrorMessageResourceType = typeof(Resources))]
public int? PrimaryTeacherId { get; set; }

This represents a teacher from a database where each teacher has a PK (int? PrimaryTeacherId) and a display name (PrimaryTeacher). I have a lookup controller like:

public class TeacherLookupController : LookupController
{
 [HttpPost]
 public ActionResult LookupList(string search)
 {
  ViewData["structure"] = new LookupListInfo
                           {
                            Key = "Id",
                            Columns = new[] {"LastName", "FirstName", "Role", "Email", "Department"},
                            Captions = new[]
                              {
                               Resources.LastName, Resources.FirstName, Resources.Role, Resources.Email, Resources.Department
                              },
                           };

  IOfficeRepository repository = Factory.CreateRepository();
  return View("Awesome/LookupList", repository.GetTeachers(search));
 }

 public ActionResult Get(int id)
 {
  IOfficeRepository repository = Factory.CreateRepository();
  Teacher teacher = repository.GetTeacher(id);
  return Content(teacher == null ? "" : teacher.DetailedName);
 }
}

Finally, my view contains a form like:

<% using (Html.BeginForm()) { %>
<div>
 <fieldset>
 ...
  <div class="editor-label">
  <%: Html.LabelFor(m => m.PrimaryTeacher) %>
 </div>
 <div class="editor-field">
  <%: Html.LookupFor(m => m.PrimaryTeacherId, controller:"TeacherLookup") %>
  <%: Html.ValidationMessageFor(m => m.PrimaryTeacher) %>
 </div>
...

This works as expected for the PrimaryTeacherId property, which contains the record's PK after selection, but it does NOT work for the PrimaryTeacher property, which is not filled with the name as displayed in the text box. In a previous version of the code I was binding to property PrimaryTeacher and not PrimaryTeacherId, which was filled automatically; but I've now discovered that with the latest version doing so leaves the teacher ID property null and rather stores the ID (as a string) in the PrimaryTeacher property, where I'd expect the name (as displayed). So, what's the correct way of binding to the model the ID and the display name?

Thanks!

Coordinator
Dec 11, 2010 at 9:44 AM

Hi, Thank You

can you tell me the version number that you were previously using?

from your code I think that the PrimaryTeacherId is going to get filled with the selected Id (Key="Id")

and in the textbox the DetailedName is going to be shown (but this is just informative, it's not going in any property)

since you have the Id of the Teacher, you can get it's name or anything else if you need to

Dec 11, 2010 at 9:55 AM

I think I was using 1.3.1, i.e. the latest version before this.

Your assumption is correct, it's the PrimaryTeacherId I want filled with the PK and this works fine. Anyway, I wanted to avoid another trip to the database (as I'm doing right now) just to retrieve the same bit of data I already got in the model used during lookup, this is why I was expecting the lookup library to fill also the name property. If this is by design anyway I can just remove the PrimaryTeacher property altogether and do my queries after the post.

Thanks!

Coordinator
Dec 11, 2010 at 9:59 AM

:D I don't know how you had this kind of behavior in 1.3.1 cuz there are no changes for the lookup from 1.3.1 to 1.4

Dec 11, 2010 at 10:06 AM

Ok thank you for the explanation :)! Maybe the model was filled by the MVC model binder in some earlier version of my pages... Just wanted to be sure I'm using the library the way it was meant. Thanks!