MvcScaffold 未正确创建我的相关下拉菜单

发布于 2024-10-21 09:10:16 字数 1216 浏览 1 评论 0原文

MvcScaffold 版本:0.9.7

好的,MvcScaffold 在我的 _CreateOrEdit.cshtml 部分视图中为我生成此代码:

<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(),
        Selected = (Model != null) && (option.LocationId == Model.LocationId)
    }), "Choose...")
    @Html.ValidationMessageFor(model => model.LocationId)
</div>

然而,这会生成以下 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>

正如您将看到的下拉“文本”与“值”显示的一样。

显然,我每次都可以通过执行以下操作来手动重新修改此代码:

Text = Html.DisplayTextFor(_ => option.LocationName).ToString(), 

...但我想解决此问题以避免这种情况。

有人可以提供任何指导吗?

谢谢保罗

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(),
        Selected = (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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薔薇婲 2024-10-28 09:10:16

我相信问题是 MvcScaffolding 不知道什么属性应该代表您的文本字段。模板尝试在类中查找它认为可能代表具有代表“文本”属性的值的列的候选属性。我看过代码,它会查找“名称”、“标题”等内容。如果找到其中之一,它将使用该字段作为文本属性。下面是它使用的实际代码:

static string[] displayPropertyNames = new[] { "Name", "Title", "LastName", "Surname", "Subject", "Count" };

它将使用它找到的第一个代码,如果根本没有找到任何代码,您将获得当前拥有的代码。这并不理想,因为您可能没有这些名称中的任何一个和/或您可能有一个包含这些名称之一的字段,但可能不希望它代表下拉列表中的文本值。

应该解决此问题的另一个选项是创建一个分部类,并在要绑定到下拉列表的模型类型上附加 DisplayColumn 属性。例如:

[DisplayColumn("LocationName")] publicpartial class DropDownBoundType {}

然而,我发现部分类的实现导致了一些其他问题,其中 MvcScaffolding 生成器停止将字段识别为关联键。因此,在某些情况下,它会生成文本框,而不是下拉菜单。还不确定问题是什么,也许它不会影响您,但不幸的是,这就是我所遇到的行为,因此请谨慎行事。

我还注意到您运行的是 9.7,并且有一个新版本 9.8。我不认为它解决了您的问题,但它解决了其他一些问题。也许值得您花时间继续更新它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文