【问题标题】:Receiving null value in search text box在搜索文本框中接收空值
【发布时间】:2011-12-02 22:29:19
【问题描述】:

我正在尝试创建一个搜索框来在数据库中查找发票。搜索框代码如下:

@using (Ajax.BeginForm("Search", "Invoice", new AjaxOptions() { HttpMethod = "POST" })) 

{

    <% input id="search-field" name="search" type="text" value="" %/>

    <% input id="search-submit" name="search-submit" type="submit" value=""   %/>

}

public ActionResult Search(FormCollection collection)
{

        if (collection["search-field"] == null)
            return RedirectToAction("Index");
        else
        {
            string id = collection["search-field"].ToString();
            return RedirectToAction("Details", "Invoice", id.Trim());
        }
}

现在的问题是我只收到 null 值控制器搜索操作。

我正在使用 MVC3 和 .NET 框架 4.0

在下一个动作中捕获字符串时,我仍然无法接收到字符串值:

public ActionResult 详细信息(字符串 id) {

  if(string.IsNullOrEmpty(id))

  return RedirectToAction("Index"); ==============> Here

  ObjectParameter[] parameters = new ObjectParameter[3];

  parameters[0]= new ObjectParameter("CUSTNMBR", id);
  parameters[1] = new ObjectParameter("StartDate", System.DateTime.Now.Date.AddDays(-90));
  parameters[2] = new ObjectParameter("EndDate", System.DateTime.Now.Date);

  return View(_db.ExecuteFunction<Models.Invoices>("uspGetCustomerInvoices", parameters).ToList<Models.Invoices>());

}

【问题讨论】:

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


    【解决方案1】:

    主要问题是您正在根据input 元素的id 而不是它们的name 属性搜索FormCollection。尝试像这样编写代码:

    查看:

    @using (Ajax.BeginForm("Search", "Invoice", new AjaxOptions() { HttpMethod = "POST" })) 
    
    {
    
        <input id="search-field" name="search" type="text" value="" />
    
        <input id="search-submit" name="search-submit" type="submit" />
    
    }
    

    行动:

    public ActionResult Search(string search)
    {    
        if (string.IsNullOrEmpty(search))
            return RedirectToAction("Index");
        return RedirectToAction("Details", "Invoice", search.Trim());
    }
    

    我修改了你的操作,所以你不再需要查询FormCollection

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-19
      • 2019-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-15
      • 1970-01-01
      相关资源
      最近更新 更多