0

我已经经历了一大堆关于我的问题的 SO 问题,但我无法找到解决方案,所以我决定发布这个问题。问题是,FormColection fc当我通过单击来自_Sidebar.cshtml. 我在这里想念什么?

代码部分:
_MyLayout.cshtml:

@using (Ajax.BeginForm("Results", "Search", null, new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "d_content", LoadingElementId = "spinner", OnBegin = "ToggleContent", OnComplete = "ToggleContent" },null))
{
    <div class="wrapper row-offcanvas row-offcanvas-left">
        <div id="a_sidebar">
            @{ Html.RenderAction("_Sidebar"); }
        </div>
        <aside class="right-side">
            <section class="content">
                <img id="spinner" src="~/Content/img/lightbox-ico-loading.gif" style="display: none;">
                <div id="d_content">
                    @RenderBody()
                </div>
            </section>
        </aside>
    </div>
}

_Sidebar.cshtml:

<ul class="treeview-menu">
                <li>
                    <div class="form-group">
                        <label>F1</label> 
                        @Html.ListBox("ddF1", (MultiSelectList)ViewData["ddF1"])
                    </div>
                    <div class="form-group">
                        <label>F2</label>
                        @Html.ListBox("ddF2", (MultiSelectList)ViewData["ddF2"])
                    </div>
                    <div class="form-group">
                        <label>Status</label>
                        @Html.ListBox("ddStatusFilter", (MultiSelectList)ViewData["ddStatusFilter"])
                    </div>
                    <div class="form-group">
                        <label>Name</label>
                        @Html.TextBox("tbName")
                    </div>
                </li>
            </ul>

<li class="treeview">

            <div style="text-align: center;">
                <button type="reset" class="btn btn-warning">Clear Filters</button>
                @Ajax.ActionLink("Search", "Results", "Search", new { }, new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "d_content", LoadingElementId = "spinner", OnBegin = "ToggleContent", OnComplete = "ToggleContent" }, new { @class = "btn btn-success", @style = "color: white;" })
            </div>
        </li>

SearchController.cs:

[HttpPost]
[AcceptVerbs(HttpVerbs.Post)]
public PartialViewResult Results(FormCollection fc)
{
    //TODO: Implement filtering through FormCollection in passed parameter
    //      but that freakin' fc var is empty
    var model = db.vw_MainTable.ToList();
    return PartialView("_SearchResultGrid", model);
}

非常感激。

4

2 回答 2

1

从代码来看,您似乎不需要覆盖整个 html 的表单标签。你可以把它放在_Sidebar.cshtml这样的地方。

还用提交按钮替换操作链接(检查下面的代码)。

@using (Ajax.BeginForm("Results", "Search", null, new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "d_content", LoadingElementId = "spinner", OnBegin = "ToggleContent", OnComplete = "ToggleContent" },null))
{
             <ul class="treeview-menu">
                <li>
                    <div class="form-group">
                        <label>F1</label> 
                        @Html.ListBox("ddF1", (MultiSelectList)ViewData["ddF1"])
                    </div>
                    <div class="form-group">
                        <label>F2</label>
                        @Html.ListBox("ddF2", (MultiSelectList)ViewData["ddF2"])
                    </div>
                    <div class="form-group">
                        <label>Status</label>
                        @Html.ListBox("ddStatusFilter", (MultiSelectList)ViewData["ddStatusFilter"])
                    </div>
                    <div class="form-group">
                        <label>Name</label>
                        @Html.TextBox("tbName")
                    </div>
                </li>
           </ul>    
           <li class="treeview">
            <div style="text-align: center;">
                <button type="reset" class="btn btn-warning">Clear Filters</button>
                <button type="submit" class="btn"> Search</button>
            </div>
           </li
}
于 2014-06-10T08:42:45.357 回答
1

这可能有多种原因。就我而言,这是因为我的表单字段只有 ID 属性而不是 Name 属性。

将 Name 属性添加到输入字段后,Controller 可以很好地看到它们。

于 2018-02-22T22:04:26.193 回答