Asp.net mvc 通过RouteValueDictionary发送单个值

Asp.net mvc 通过RouteValueDictionary发送单个值,asp.net-mvc,asp.net-mvc-3,razor,routevalues,Asp.net Mvc,Asp.net Mvc 3,Razor,Routevalues,我的ViewModel的一个属性是数组,不幸的是,每当我发回控制器时,它都是null。我想出了一个简单的方法,将值放入一个逗号分隔的字符串中 这对我们的分页插件非常有用,它使用RouteValueDictionary发回我们的Index方法。但是,它在Html.BeginForm帮助程序中不起作用,该帮助程序会发回不同的控制器操作(“code>更新”方法) 查看 @*Since we can't send arrays or complex objects break array down in

我的ViewModel的一个属性是数组,不幸的是,每当我发回控制器时,它都是
null
。我想出了一个简单的方法,将值放入一个逗号分隔的字符串中

这对我们的分页插件非常有用,它使用
RouteValueDictionary
发回我们的
Index
方法。但是,它在
Html.BeginForm
帮助程序中不起作用,该帮助程序会发回不同的控制器操作(“code>更新”方法)

查看

@*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>

}

由于您正在执行post,将其发送到后端的最简单方法是添加一个隐藏字段:

@Html.HiddenFor("searchChannelCodesPaging", searchChannelCodesPaging);
作为路由值,您可能需要通过以下两种方法之一在控件中显式获取它。这些对象可以在控制器类中直接访问

RouteData.Values("searchChannelCodesPaging")
Request.QueryString.Get("searchChannelCodesPaging");

不必将数组类型模型参数序列化为CSV字符串,即可将其发布到控制器。您可以这样做:

@for (var i = 0; i < Model.searchChannelCode.Length; i++)
{
    @Html.HiddenFor(m => m.searchChannelCode[i]);
}
for(var i=0;im.searchChannelCode[i]); }
HiddenForhelper似乎不喜欢这种格式(我假设代码中的第二个参数是我在视图中创建的以coma分隔的
var channelCodes?)。我需要以某种方式分配
var
吗?没关系,我知道了:
@Html.Hidden(“searchChannelCodesPaging”,channelCodes)
我如何解释控制器参数中
HiddenFor
通道代码的动态数量?一个参数
IList searchChannelCode
@for (var i = 0; i < Model.searchChannelCode.Length; i++)
{
    @Html.HiddenFor(m => m.searchChannelCode[i]);
}