我的 ViewModel 的属性之一是一个数组,不幸的是,null
每次我回发到控制器时它都会出现。我想出了一个简单的技巧,将值放入逗号分隔的字符串中。
这对我们的分页插件非常有用,它Index
使用RouteValueDictionary
. 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
变量每次都是空的。
[HttpPost]
public ActionResult Update(ZipCodeIndex updateZip, string button, string searchChannelCodesPaging)
{