使用数组来填充MVC多个字段/ C# [英] Using an array to populate multiple fields with MVC/C#

查看:174
本文介绍了使用数组来填充MVC多个字段/ C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过我看到很多这个问题变化的网站搜索,但没有涉及到这种特殊情况下。

Searching through the site I see a lot of variations of this question, but none of them relate to this particular case.

我想在索引i 从我的模型中包含字符串数组。

I am trying to populate multiple DropDownLists with the value at index i from an array which holds strings within my model.

我的模板是类似的(简体)于以下内容:

My template is similar (simplified) to the following:

@model foo

@for (int i = 0; i < Model.ArrayName.Length; i++ )
{
     @Html.LabelFor(m => m.ArrayName[i], "Index " + i, new {})
     @Html.DropDownListFor(m => m.ArrayName[i], Model.ListOfPotentialValues, new {})
     @Html.ValidationMessageFor(m => m.ArrayName[i])
}

要更加清晰, arrayName中是一个字符串数组(的String [] ), ListOfPotentialValues​​ 是类型的IEnumerable&LT; SelectListItem&GT;

To be more clear, ArrayName is a string array (string[]), ListOfPotentialValues is of the type IEnumerable<SelectListItem>

该模型的code可能会看到如下:

The code for the model may be seen below:

public IEnumerable<SelectListItem> ListOfPotentialValues
{
    return SelectList(/* A list of Strings*/)
}

public string[] ArrayName { get; set; }

//Constructor
public foo()
{
    ArrayName = new string[] {"Foo", "Foo", "Baz","Qux"};
}

该视图发送第一个下拉列表下面的HTML输出(这是一贯的对所有的)

The view sends the following HTML output for the first dropdown (which is consistent for all of them)

<div>
    <label for="ArrayName_0_">Index 0</label>
    <select id="ArrayName_0_" name="ArrayName[0]">
        <option>Foo</option>
        <option>Bar</option>
        <option>Baz</option>
        <option>Qux</option>
    </select>
    <span class="field-validation-valid" data-valmsg-for="ArrayName[0]" data-valmsg-replace="true"></span>
</div>

这个,你可能会或可能不会推导出 arrayName中是的,当然,数组名拿着我想要显示的值和 ListOfPotentialValues​​ 是可以选择的值的的SelectList 。值的列表显示出来,但它会如果我不使用数组没有设置它的值。

From this, you may or may not have derived that ArrayName is, of course, the name of the array holding the values I want displayed and ListOfPotentialValues is a SelectList of values which may be selected. The list of values is showing up, but not setting its value as it would if I were to not use an array.

虽然下拉值不填充,我可以,如果我称之为 @ Model.ArrayName [I] 我的MVC模板内,以显示它的价值。

While the value for the dropdown does not populate, I am able to display the value of it if I call @Model.ArrayName[i] within my MVC Template.

更多细节:
如果 ListOfPotentialValues​​ 包含的值富,酒吧,巴兹,Qux
arrayName中包含的值富,富,Qux,巴兹

More detail: If ListOfPotentialValues contains the values Foo, Bar, Baz, Qux And ArrayName contains the values Foo, Foo, Qux, Baz

我预计下拉菜单开始与价值观富,富,Qux,巴兹(分别),同时还具有选项选择美孚,酒吧,巴兹和Qux

I would expect the dropdowns to start with the values Foo, Foo, Qux, Baz (respectively) while still having the option to select Foo, Bar, Baz, and Qux

有没有一些简单的我失踪?

Is there something simple that I'm missing?

推荐答案

不幸的是这不会工作 - DropDownListFor()在使用时的作用有点不同于其他佣工一个循环(它被提出作为codePLEX问题了几次,我不知道如果它的一个bug或只是一个辅助的限制)。

Unfortunately this wont work - the DropDownListFor() works a little differently from the other helpers when used in a loop (its been raised as an issue on CodePlex a few times and I'm not sure if its a bug or just a limitation of the helper).

您需要使用一个自定义的 EditorTemplate 为你的模型并通过的SelectList 来的模板附加视图数据(但是,在你的情况不恰当的,因为你只有值类型的数组),或者你需要在每个迭代中构建一个新的的SelectList ,并使用重载设置值。例如,您模型需要一个属性,它是对象的集合(不是的SelectList

You need either a custom EditorTemplate for you model and pass the SelectList to the template as additional view data (but that not appropriate in your case since you only have an array of value types) or you need to construct a new SelectList in each iteration and use the overload that sets the Selected value. For example, you model needs a property which is a collection of objects (not a SelectList)

@Html.DropDownListFor(m => m.ArrayName[i], new SelectList(Model.MyCollection, "yourValueProperty", "yourTextProperty", Model.ArrayName[i])

这篇关于使用数组来填充MVC多个字段/ C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆