我使用与具有与一个隐藏字段的格式,并提交按钮自定义列的WebGrid。看看HTML源代码,我发现隐藏的字段有一个值。表单在控制器中提交并击中正确的HttpPost方法。问题是隐藏的领域不符合职位。
守则
这里是生成的WebGrid的代码:
WebGrid wgSearchResults = new WebGrid(
source: Model.notes,
rowsPerPage: 10,
canPage: true,
canSort: true
);
wgSearchResults.Pager(WebGridPagerModes.All, numericLinksCount: 10);
@wgSearchResults.GetHtml(
htmlAttributes: new { id = "wgSearchResults" },
mode: WebGridPagerModes.All,
columns: wgSearchResults.Columns(
wgSearchResults.Column(format: (item) =>
{
System.Text.StringBuilder html = new System.Text.StringBuilder();
html.Append("<form action='/ManageData/ViewFacilities' method='post'>");
html.Append("<input type='hidden' id='hidBusinessId_' value='" + item.businessId.ToString() + "' />");
html.Append("<input type='submit' id='btnViewFacilities_" + item.ID.ToString() + "' name='btnSubmit_" + item.ID.ToString() + "' value='View Facilities' />");
html.Append("</form>");
return new HtmlString(html.ToString());
},
canSort: false
),
wgSearchResults.Column(columnName: "note", header: Sorter("note", "Note", wgSearchResults), canSort: true)
)
)
的Model.notes结构是这个类的一个实例列表:
public class SearchNotesResult
{
public int ID { get; set;}
public int businessId { get; set; }
public string note { get; set; }
}
控制器此按钮命中的方法是:
[HttpPost]
public ActionResult ViewFacilities(string hidBusinessId)
{
int businessId = 0;
bool isValidNumber = false;
isValidNumber = int.TryParse(hidBusinessId, out businessId);
if (isValidNumber)
{
if (businessId > 0)
{
TempData["BusinessId"] = businessId;
return RedirectToAction("GoHere");
}
else
return RedirectToAction("DontGoHere");
}
else
{
return RedirectToAction("DontGoHere");
}
}
在上述方法中,hidBusinessId值为空。
企图
- 添加参数
FormCollection form
到上述方法(只包含在提交按钮) - 重构使用剃刀语法(
@
与<text>
标签)此列。没有运气。 - 研究小时。这就是我来这里的原因。
的选项
- 改变这个帖子的获取(和不同的控制一样
Html.ActionLink
) - 添加业务ID到按钮的ID和方法分析
- 任何其他社区建议
谢谢大家的时间!
您隐藏的输入没有'name'属性,因此没有任何内容可以发布(删除无效的'id'属性并使用'name ='hidBusinessId'')。你也可以从提交按钮中删除无意义的'id'和'name'属性。因为它的一个'int' - 那么方法中的参数应该是'int hidBusinessId',而不是'string' –