MvcScaffold not creating my related dropdowns correctly
MvcScaffold Version: 0.9.7
Okay, so MvcScaffold generates this code for me in my _CreateOrEdit.cshtml partial view:
<div class="editor-field">
@Html.DropDownListFor(model => model.LocationId, ((IEnumerable<JobSite.Models.Location>)ViewBag.PossibleLocations).Select(option => new SelectListItem {
Text = Html.DisplayTextFor(_ => option).ToString(),
Value = option.LocationId.ToString(),
Se开发者_如何转开发lected = (Model != null) && (option.LocationId == Model.LocationId)
}), "Choose...")
@Html.ValidationMessageFor(model => model.LocationId)
</div>
This however generates the following HTML:
<select data-val="true" data-val-number="The field LocationId must be a number." data-val-required="The LocationId field is required." id="LocationId" name="LocationId"><option value="">Choose...</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
As you will see the drowndown "text" is being displayed the same as the "value".
Obviously I can re-touch this code manually each time by doing:
Text = Html.DisplayTextFor(_ => option.LocationName).ToString(),
...but I would like to fix the issue to avoid this.
Can anyone offer any guidance?
Thanks Paul
I believe the problem is MvcScaffolding does not know what property is supposed to represent your text field. The templates try to find a candidate property in the class that it thinks may represent a column that would have a value representative of the "text" property. I've seen the code and it looks for things like "Name", "Title" and etc. If it finds one of those it will use that field for the text property. Here is the actual code it uses:
static string[] displayPropertyNames = new[] { "Name", "Title", "LastName", "Surname", "Subject", "Count" };
It will use the first one it finds and if it doesn't find any at all you get the code you have currently. This is not ideal because you may not have either of those names and/or you may have a field with one of those names but may not want it representing the text value in the drop down.
The other option that is supposed to address this concern is creating a partial class and attaching a DisplayColumn attribute on the model type you are binding to the drop down. For example:
[DisplayColumn("LocationName")] public partial class DropDownBoundType {}
What I have found however is the implementation of a partial class is causing some other issues where the MvcScaffolding generator stops recognizing fields as association keys. So instead of having drop downs it produces text boxes in some cases. Not sure what the issue is yet and maybe it won't affect you but unfortunately that is the behavior I am getting so tread cautiously.
Also I noticed you were running 9.7 and there is a new version available 9.8. I don't think it addresses your issue but it addresses some others. It may be worth your time to go ahead and update it.
精彩评论