【问题标题】:Search two parameters with Html.BeginForm()使用 Html.BeginForm() 搜索两个参数
【发布时间】:2018-12-07 21:09:15
【问题描述】:

应用程序有标签: 所有、电脑、电话

产品搜索应该依赖于给定的标签,也就是说,如果我们浏览手机标签,搜索时只有手机应该显示给我们。

类:

产品:

 

public int Id {get; set; }
        public string Name {get; set; }
        public decimal Price {get; set; }
        public int CategoryId {get; set; }

类别:

 

public int Id {get; set; }
        public string Name {get; set; }

查看:

 @using (Html.BeginForm ("Index", "Product", FormMethod.Get, null))
    {
    div class="input-group"
    input id="search" type="search" class="form-control" name="search" 
    placeholder= "Search ...">
    button class="btn btn-default">Search button>
    div>

    } 

 

                             

【问题讨论】:

  • @Johan 的 answer 会很有帮助。

标签: c# asp.net-mvc


【解决方案1】:

产品控制器的索引操作也应该接收 CategoryId

public ActionResult Index(string search, int categoryId)
{
}

表单应该发送 categoryId 和隐藏字段

@using (Html.BeginForm ("Index", "Product", FormMethod.Get, null))
{
  <input id="search" type="text" name="search">
  <input type="hidden" id="categoryId" value="1" name="search">
  <button type="submit">Search</button>    
} 

检测选项卡更改并更改 categoryId 值

例如:使用 Jquery

$("#categoryId").val(2);

【讨论】:

  • 谢谢大家的回答!我做了不同的事情。在控制器中,从我添加到 ViewModel 的书签中调用的类别被发送回视图。从我添加到隐藏输入的对象。 value = "@ Model.CurrentCategory" name = "category">
猜你喜欢
  • 1970-01-01
  • 2013-10-14
  • 1970-01-01
  • 2013-03-16
  • 1970-01-01
  • 2019-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多