2

これが私のコードです:

モデル

public class InformationsModel
{
    public List<InformationModel> infos { get; set; }

    public InformationsModel()
    {
    }
}

public class InformationModel
{
    public InformationModel() {

    }

    public string Name { get; set; }
    public string Value { get; set; }
    public string Group { get; set; }
    public string Type { get; set; }
    public bool Avaiable { get; set; }    
}

意見

@model InterfaceWeb.Models.InformationsModel


@using (Html.BeginForm("UpdateInformations", "Main", FormMethod.Post, new { @id = "frmInformations" }))
{    
    @Html.EditorFor(x => x.infos)                
}

テンプレート

@model InterfaceWeb.Models.Information.InformationModel

<div class="infoMain">
    <div class="colunas">
        @Html.TextBoxFor(x => x.Name, new { style = "width:250px;" })
    </div>
    <div class="colunas">
            @Html.TextBoxFor(x => x.Value, new { style = "width:120px;" })
    </div>
    <div class="colunas">
        @Html.TextBoxFor(x => x.Group, new { style = "width:120px;" })
    </div>
    <div class="colunas">
        @Html.TextBoxFor(x => x.Type, new { style = "width:150px;" })
    </div>
    <div class="colunas">
        @Html.CheckBoxFor(x => x.Avaiable, new { style = "width:10px; margin-left:20px;" })
    </div>
</div>

コントローラ

[HttpPost]
public ActionResult UpdateInformations(InformationsModel infos)
{                

}

コントローラーに到達すると、モデルが空になり、その理由がわかりません。

テンプレート、ビューを変更し、リストを初期化した後、前に変更しようとしましたが、何も機能しませんでした..

助けてくれてありがとう!=)

4

2 に答える 2

2

モデルの接頭辞が機能する方法が原因で、この問題が発生しています。具体的には、問題は次のとおりです。

[HttpPost]
public ActionResult UpdateInformations(InformationsModel infos)
{             

}

テンプレートが生成する HTML を見ると、次のようになっていることがわかります。

<input id="infos_0__Name" name="infos[0].Name" type="text" value="" />

この場合、知っておくべき重要なことは、プロパティinfosの値に関連付けられているプレフィックスです。InformationsModel.infoscontroller でパラメーターにも名前を付けることでinfos、モデル バインダーを捨てることになります。値を取得するには、次のように名前を変更するだけです。

[HttpPost]
public ActionResult UpdateInformations(InformationsModel model)
{             

}

余談ですが、 など、InformationModelより適切な名前に変更することをお勧めします。クラス名がほとんど同じであり、名前が実際にはその意図を伝えていないという理由だけで、あなたがしようとしていたことに従うのは少し困難でした.PersonEmployee

于 2013-09-30T16:25:16.240 に答える