2017-04-15 131 views
0

我在ASP.net MVC应用程序工作时到达不绑定,我有一个产品表作为截图所示:视图模型属性在控制器

table of products

我希望能够过滤该产品表,并且我希望通过查询字符串参数(作为GET)进行过滤,以便可以共享该URL。

视图模型的页面是这样的:

public class InventoryReportViewModel 
{ 
    public SearchViewModel Search { get; set; } // 2 string props [Type and Term] 
    public IEnumerable<ProductViewModel> Products { get; set; } 
    public PaginationViewModel Pagination { get; set; } // 3 int props [currentPage, recordsPerPage, totalRecords] 
} 

我用剃刀助手绘制滤波器的输入,如:

@Html.EditorFor(m => m.Search.Term, new { htmlAttributes = new { @class = "form-control" } }) 

同时,我已经建立了我形成使用GET像这样:

@using (Html.BeginForm("Inventory", "Report", FormMethod.Get)) 
{ 
    // form elements 
} 

ReportController.cs具有以下甲基OD,是有关在这里我的问题:

public ActionResult Inventory(string SearchTerm, string SearchType, int page = 1) 
{ 
    var viewModel = _reportService.GetProducts(page, SearchTerm, SearchType); 
    return View(viewModel); 
} 

当我通过一个搜索词,然后单击筛选结果按钮,我到达我上面的控制器的方法,但SearchTermSearchTypenull

我知道如何“破解”这个工作,例如,如果我这样做:

<input type="text" name="SearchTerm" class="form-control"/> 

然后搜索词我输入将由控制器回升,但有没有其他办法?

回答

1

既然你已经做了viewmodelSearch

public SearchViewModel Search { get; set; } 

你只需要把它传递给控制器​​这样

public ActionResult Inventory(SearchViewModel Search, int page = 1 
{ 
    var viewModel = _reportService.GetProducts(page, Search.Term, Search.Type); 
    return View(viewModel); 
} 

你越来越null因为textboxes被命名为Search.Term是为什么它不符合参数。

+0

感谢@Usman,工作。 – Ciwan

0

的形式应该是后

@using (Html.BeginForm("Inventory", "Report", FormMethod.Post)) 
{ 
    // form elements 
} 

这也可以是清洁:

@Html.EditorFor(m => m.Search.Term, new { htmlAttributes = new { @class = "form-control" } }) 

@Html.EditorFor(m => m.Search.Term, new { @class = "form-control" }) 

另一个问题, 在Razor视图,你有第一行指定模型?