Confusion with DropDownListFor<>
So what I am trying to do is import data from a database into a MVC2 DropDownListFor.
What I essentially have going is a roleplaying guild site I am working on for a friend. What the page should be doing is allowing a person to edit 开发者_开发技巧their character. So what is being passed into the page is the model containing the characters information. Then I have the view making a call to a method in the YourCharacterEdit model to get the information on a player character's race.
So I have tried to follow lots of different ideas that I have found from various tutorials online but nothing seems to be working. I am pretty much open to whatever ideas anyone might have at this point as to how I call fill up the drop down list. Currently I don't have much of a model to show because I keep scraping whatever I come up with.
The Controller:
if (Request.IsAuthenticated)
{
UserRepository _urepos = new UserRepository();
CharacterRepository _crepos = new CharacterRepository();
var check = _urepos.GetSpecificUserByName(User.Identity.Name);
var yourchar = _crepos.GetSpecificCharacter(CharID);
if (yourchar.CharUserRef == check.UserID)
{
//
YourCharacterEdit VarToReturn = new YourCharacterEdit();
VarToReturn.EmployeeID = yourchar.EmployeeID;
VarToReturn.CharFirstName = yourchar.CharFirstName;
VarToReturn.CharLastName = yourchar.CharLastName;
VarToReturn.CharGender = yourchar.CharGender;
VarToReturn.CharSpecies = yourchar.CharSpecies;
VarToReturn.CharDescription = yourchar.CharDescription;
VarToReturn.CharBackground = yourchar.CharBackground;
VarToReturn.CharJob = yourchar.CharJob;
return View("CharEdit", VarToReturn);
}
else
{
return View("401");
}
}
The View:
<div class="editor-field">
<%: Html.DropDownListFor(model => model.CharSpecies, ?)%>
<%: Html.ValidationMessageFor(model => model.CharSpecies) %>
</div>
So anyone have any good methods for getting the ListBox propagated?
Also the DB is access through guildEntities. The table is TBLCharS. The fields needed inside that table are CharS_id and CharS_name.
The first thing to do is define your model. The view should not need any additional data other than what you pass it in the model. Build your model and fill it up before calling the view.
My implementation is a bit overkill but here it goes:
In the model, create a container to hold the dropdown data:
public IEnumerable<SelectListItem> LocationList { get; set; }
In the controller, populate your model including the dropdown list:
model.LocationList = repository.GetLocationsSelectList(selectedLocationId);
I use a repository and Linq to grab the data from the database:
var q = (from l in Repository.For<LocationEntity>()
select new
{
RowId = l.RowId,
LocationString = l.Name,
});
var result = q.ToSelectList(a => a.RowId.ToString(), a => a.LocationString, a => a.RowId == locationId);
return result;
The ToSelectList extension I grabbed from somewhere (I forgot where):
public static class EnumerableExtensions
{
/// <summary>
/// Converts the source sequence into an IEnumerable of SelectListItem
/// </summary>
/// <param name="items">Source sequence</param>
/// <param name="nameSelector">Lambda that specifies the name for the SelectList items</param>
/// <param name="valueSelector">Lambda that specifies the value for the SelectList items</param>
/// <returns>IEnumerable of SelectListItem</returns>
public static IEnumerable<SelectListItem> ToSelectList<TItem, TValue>(this IEnumerable<TItem> items, Func<TItem, TValue> valueSelector, Func<TItem, string> nameSelector)
{
return items.ToSelectList(valueSelector, nameSelector, x => false);
}
/// <summary>
/// Converts the source sequence into an IEnumerable of SelectListItem
/// </summary>
/// <param name="items">Source sequence</param>
/// <param name="nameSelector">Lambda that specifies the name for the SelectList items</param>
/// <param name="valueSelector">Lambda that specifies the value for the SelectList items</param>
/// <param name="selectedItems">Those items that should be selected</param>
/// <returns>IEnumerable of SelectListItem</returns>
public static IEnumerable<SelectListItem> ToSelectList<TItem, TValue>(this IEnumerable<TItem> items, Func<TItem, TValue> valueSelector, Func<TItem, string> nameSelector, IEnumerable<TValue> selectedItems)
{
return items.ToSelectList(valueSelector, nameSelector, x => selectedItems != null && selectedItems.Contains(valueSelector(x)));
}
/// <summary>
/// Converts the source sequence into an IEnumerable of SelectListItem
/// </summary>
/// <param name="items">Source sequence</param>
/// <param name="nameSelector">Lambda that specifies the name for the SelectList items</param>
/// <param name="valueSelector">Lambda that specifies the value for the SelectList items</param>
/// <param name="selectedValueSelector">Lambda that specifies whether the item should be selected</param>
/// <returns>IEnumerable of SelectListItem</returns>
public static IEnumerable<SelectListItem> ToSelectList<TItem, TValue>(this IEnumerable<TItem> items, Func<TItem, TValue> valueSelector, Func<TItem, string> nameSelector, Func<TItem, bool> selectedValueSelector)
{
foreach (var item in items)
{
var value = valueSelector(item);
yield return new SelectListItem
{
Text = nameSelector(item),
Value = value.ToString(),
Selected = selectedValueSelector(item)
};
}
}
}
And finally, in your view:
<%: Html.LabelFor(m => m.LocationId)%>
<%: Html.DropDownListFor(m => m.LocationId, Model.LocationList, "<-- Select One -->")%>
<%: Html.ValidationMessageFor(m => m.LocationId)%>
Add a comment if you have any questions or need more code.
精彩评论