1

私の ViewModel のプロパティの 1 つは、残念ながらnullコントローラーにポストバックするたびに配列です。値をカンマ区切りの文字列に配置する簡単なハックを考え出しました。

これはIndexRouteValueDictionary. ただし、別のコントローラー アクション (メソッド) Html.BeginFormにポスト バックするヘルパーでは機能しません。Update

意見

@*Since we can't send arrays or complex objects break array down into string for RouteValueDictionary*@
var channelCodes = "";
for (int i = 0; i < Model.searchChannelCode.Length; i++)
{
    channelCodes += Model.searchChannelCode[i];

    if (i + 1 < Model.searchChannelCode.Length)
    {
        channelCodes += ",";
    }
}  

@*The 'searchChannelCodesPagin' variable from this RouteValueDictionary always posts back as null
using (Html.BeginForm("Update", "ZipCodeTerritory", new RouteValueDictionary()
    {
        {"searchChannelCodesPaging", channelCodes }
    }, FormMethod.Post, new {id = "UpdateForm"}))
{
    @Html.HiddenFor(model => model.searchZip)
    @Html.HiddenFor(model => model.searchTerritory)
    @Html.HiddenFor(model => model.searchState)
    @Html.HiddenFor(model => model.searchActiveOnly)
    @Html.HiddenFor(model => model.zipCodeTerritory)

    <div id="cloneBox">
        <div id="rw1">
            @Html.LabelFor(model => model.newTerritory)
            @Html.TextBoxFor(model => model.newTerritory, new { style = "width: 30px;padding-left:10px;", maxLength = 3 })
            @Html.LabelFor(model => model.newDescription)
            @Html.TextBoxFor(model => model.newDescription, new { style = "width: 250px;padding-left:10px;", maxLength = 30 })  
            @Html.LabelFor(model => model.newEffectiveDate)     
            @Html.TextBoxFor(model => model.newEffectiveDate, new { style = "width: 80px;padding-left:10px;" }) 
            <div id="rw2" style="padding-top: 10px;">
                @Html.LabelFor(model => model.newChannelCode)
                @Html.DropDownListFor(model => model.newChannelCode, Model.ChannelCodes, " ")
                @Html.LabelFor(model => model.newStateCode)
                @Html.DropDownListFor(model => model.newStateCode, Model.StateCodes, "  ")
                @Html.LabelFor(model => model.newEndDate)
                @Html.TextBoxFor(model => model.newEndDate, new { style = "width: 80px;" })
            </div>                                 
        </div>
    </div>
    <br/>
    <div id="buttonDiv">
        <button type="submit" id="CloneButton" name="button" value="clone">Apply New Data</button>
        <button type="submit" id="deleteButton" name="button" value="delete">Delete Selected Items</button> 
        <button type="submit" id="removeButton" name="button" value="removeErrors">Remove Selected Errors</button>           
    </div>

}

コントローラ

上記のフォームは、このコントローラー アクションにポストします。変数はsearchChannelCodePaging毎回 null です。

    [HttpPost]
    public ActionResult Update(ZipCodeIndex updateZip, string button, string searchChannelCodesPaging)
    {
4

2 に答える 2

1

投稿を行っているので、それをバックエンドに取得する最も簡単な方法は、隠しフィールドを追加することです。

@Html.HiddenFor("searchChannelCodesPaging", searchChannelCodesPaging);

ルーティング値として、次の 2 つの方法のいずれかを使用して、コントロール内で明示的に取得する必要がある場合があります。これらのオブジェクトは、Controller クラス内で直接アクセスできます。

RouteData.Values("searchChannelCodesPaging")
Request.QueryString.Get("searchChannelCodesPaging");
于 2014-03-07T19:42:47.277 に答える
1

コントローラーに投稿するために、配列型のモデル パラメーターを CSV 文字列にシリアル化する必要はありません。代わりにこれを行うことができます:

@for (var i = 0; i < Model.searchChannelCode.Length; i++)
{
    @Html.HiddenFor(m => m.searchChannelCode[i]);
}
于 2014-03-07T19:47:24.677 に答える