【发布时间】:2017-10-21 02:55:11
【问题描述】:
问题
我正在使用一个带有自定义列的 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 值为 null。
尝试
- 在上述方法中添加参数
FormCollection form(仅包含提交按钮) - 使用 Razor 语法重构此列(
@和<text>标签)。不走运。 - 研究几个小时。这就是我在这里的原因。
选项
- 将此帖子更改为 Get(以及不同的控件,例如
Html.ActionLink) - 将业务 ID 添加到按钮 ID 并在方法中解析
- 社区提出的任何其他建议
感谢大家的时间!
【问题讨论】:
-
您隐藏的输入没有
name属性,因此没有要发布的内容(删除无效的id属性并使用name='hidBusinessId')。您也可以从提交按钮中删除无意义的id和name属性。由于它是int- 那么方法中的参数应该是int hidBusinessId,而不是string
标签: asp.net-mvc webgrid