【问题标题】:MVC Webgrid Column with Form Does Not Pass Hidden Field带有表单的 MVC Webgrid 列不传递隐藏字段
【发布时间】: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。

尝试

  1. 在上述方法中添加参数FormCollection form(仅包含提交按钮)
  2. 使用 Razor 语法重构此列(@&lt;text&gt; 标签)。不走运。
  3. 研究几个小时。这就是我在这里的原因。

选项

  1. 将此帖子更改为 Get(以及不同的控件,例如 Html.ActionLink
  2. 将业务 ID 添加到按钮 ID 并在方法中解析
  3. 社区提出的任何其他建议

感谢大家的时间!

【问题讨论】:

  • 您隐藏的输入没有name 属性,因此没有要发布的内容(删除无效的id 属性并使用name='hidBusinessId')。您也可以从提交按钮中删除无意义的idname 属性。由于它是int - 那么方法中的参数应该是int hidBusinessId,而不是string

标签: asp.net-mvc webgrid


【解决方案1】:

多么愚蠢的错误……需要 name 属性而不是 id。

html.Append("<form action='/ManageData/ViewFacilities' method='post'>");
html.Append("<input type='hidden' name='hidBusinessId' value='" + item.businessId.ToString() + "' />");
html.Append("<input type='submit' value='View Facilities' />");
html.Append("</form>");

虽然字符串数据类型有效(请参阅下面的评论)。感谢 Stephen 快速正确的回复!

【讨论】:

  • 重复的id属性是无效的html!! (并且您按钮上的 name 属性毫无意义)并将 int 发布到 string 参数然后手动转换它简直是疯了。
  • 感谢您的评论。我做了额外的调整(删除了不需要的 ID 和 Name 值),事情仍然按预期工作。如果发布方法的 hidBusinessId 参数的数据类型从 string 更改为 int,则通过将隐藏字段的值修改为非数字值并提交表单更容易引起系统错误。一个系统范围的错误处理模块可以处理这个(我稍后可能会做)。再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-20
  • 1970-01-01
  • 1970-01-01
  • 2011-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多