2

これは私のモデルです:

public class Attribute
{
    public string Key { get; set; }
    public string Value{ get; set; }
}

GET create に入力します

    public ActionResult Create()
    {
        var Attributes = new[] 
        {
            new Attribute {Key = "Name" Value = "" },
            new Attribute {Key = "Age" Value = "" },
        };
        return View(Attributes);
    }

私のビューは次のようになります。

@model IEnumerable<Customers.ViewModels.Attribute>
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    <fieldset>
        @foreach (var item in Model)
        {
            <div class="editor-label">
                @Html.Label(item.Key)
            </div>
            <div class="editor-field">
                @Html.EditorFor(m => item.Value)
                @Html.ValidationMessageFor(m => item.Value)
            </div>
        }
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

次に、My Post Create は次のようになります。

    [HttpPost]
    public ActionResult Create(IEnumerable<Attribute> attributes)
    {
    }

しかし、私IEnumerable<Attribute> attributesはnullです。助言がありますか?

4

1 に答える 1

3

のエディタテンプレートを作成してから、モデルをAttribute渡す必要があります。List<Attribute>

@Model Attribute
<div class="editor-label">
    @Html.LabelFor(m => m.Key)
</div>

<div class="editor-field">
    @Html.EditorFor(m => m.Value)
    @Html.ValidationMessageFor(m => item.Value)
</div>

あなたの見解では以下を使用してください:

<fieldset>
    @Html.EditorFor(m > m)

    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>

foreachは要素の正しい名前を作成しないため、これを行う必要があります。

于 2012-12-12T19:49:35.673 に答える