【问题标题】:MVC4: How to send form parameter from view to controller?MVC4:如何将表单参数从视图发送到控制器?
【发布时间】:2014-03-13 20:12:53
【问题描述】:

浏览操作:

public ActionResult Browse(int? Category, string sortOrder, string currentFilter, string searchString, int? page)

我可以使用操作链接发送类别 ID

@Html.ActionLink("Name", "Browse", new { sortOrder = ViewBag.NameSortParm, currentFilter=ViewBag.CurrentFilter, Category=ViewBag.CategoryId })

如何将类别 ID 从表单传递到控制器?

<div id="search">
        @using (Html.BeginForm("Browse", "Home", FormMethod.Get))
        {
        @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
        <input type="submit" value="Search" />
        }
        </div>

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4


    【解决方案1】:

    您可以在隐藏的输入中发送它:

    @using (Html.BeginForm("Browse", "Home", FormMethod.Get))
    {
        @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
        @Html.Hidden("Category",ViewBag.CategoryId)
        <input type="submit" value="Search" />
    }
    

    【讨论】:

    • 而不是这一行:@Html.Hidden("Category",ViewBag.CategoryId) 我使用了这个代码:int CategoryId = ViewBag.CategoryId; @Html.Hidden("类别", CategoryId);因为动态 ViewBag.CategoryId 有错误。
    • 投射 ViewBag 结果也是一种选择。 @Html.Hidden("Category", (int?)ViewBag.CategoryId) 为了跳过做int CategoryId = ViewBag.CategoryId;
    猜你喜欢
    • 2013-04-21
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 2020-12-26
    • 2016-03-21
    • 1970-01-01
    相关资源
    最近更新 更多