C# FormCollection在mvc中返回空值

C# FormCollection在mvc中返回空值,c#,asp.net-mvc,formcollection,C#,Asp.net Mvc,Formcollection,这段代码为字符串sc返回空值。为什么会这样??在我的视图中有一个文本框。单击按钮并检索与搜索关键字相关的数据时,我想将该值作为参数传递给SearchFromObject方法。这是我的视图 [HttpGet] public ActionResult Index() { return View(); } [HttpPost] public ActionResult Index(FormCollection fc) {

这段代码为字符串sc返回空值。为什么会这样??在我的视图中有一个文本框。单击按钮并检索与搜索关键字相关的数据时,我想将该值作为参数传递给SearchFromObject方法。这是我的视图

[HttpGet]
    public ActionResult Index()
    {

        return View();
    }

    [HttpPost]
    public ActionResult Index(FormCollection fc)
    {
        String sc = fc["SearchString"]; 
        return RedirectToAction("SearchFromObject", new { id = sc });
    }

    public ActionResult SearchFromObject(string searchString)
    {
        var Items = from m in db.Objects
                    select m;
        if (!String.IsNullOrEmpty(searchString))
        {
            Items = Items.Where(s => s.Name.Contains(searchString));
        }
        return View(Items);
    }
@{
ViewBag.Title=“搜索”;
}
搜索

@使用(Html.BeginForm())
{
标题:@Html.TextBox(“搜索字符串”)

}
指定post方法、控制器名称和表单操作,如下所示:

@{
ViewBag.Title = "Search";
}

<h2>Search</h2>
<p>

@using (Html.BeginForm())
{<p>
    Title: @Html.TextBox("SearchString") <br />
    <input type ="submit" value="Search" />
</p>
}
@使用(Html.BeginForm(“Index”,“Default1”,FormMethod.Post))
{    

标题:@Html.TextBox(“搜索字符串”)

}
您的方法

@using (Html.BeginForm("Index", "Default1", FormMethod.Post))
{    
  <p>
  Title: @Html.TextBox("SearchString") <br />
  <input type ="submit" value="Search" />
</p>
}
有一个名为
searchString
的参数,但在
Index()
POST方法中,您尝试使用
new{id=sc}
传递一个名为
id
的参数。它不是
sc
的值,它是
null
,而是第二个GET方法中
searchString
的值,它是
null

将POST方法签名更改为

public ActionResult SearchFromObject(string searchString)

可以显示视图的渲染html吗?现在我觉得还行。
@使用(Html.BeginForm())
将添加默认值,这些值与您所做的完全相同(假设控制器名称为
Default1Controller
),因此,除非您指定其他控制器或操作,否则这是不必要的
[HttpPost] public ActionResult Index(string SearchString)
{
  return RedirectToAction("SearchFromObject", new { searchString = SearchString});
}