2015-02-07 28 views
0
[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); 
    } 

此代码为String sc返回空值。为什么??在我看来,有一个文本box.i希望该值传递给SearchFromObject方法作为参数点击按钮和检索到搜索keyword..Here是我的看法FormCollection在mvc中返回空值

@{ 
ViewBag.Title = "Search"; 
} 

<h2>Search</h2> 
<p> 

@using (Html.BeginForm()) 
{<p> 
    Title: @Html.TextBox("SearchString") <br /> 
    <input type ="submit" value="Search" /> 
</p> 
} 
+0

您能不能告诉你呈现的查看HTML?现在看起来好了。 – 2015-02-07 18:52:47

回答

0

你的方法

public ActionResult SearchFromObject(string searchString) 

有一个名为searchString参数,但在Index() POST方法,您尝试使用new { id = sc }传递名为id的参数。它的值不是sc的值是null,它的值在searchString的第二个GET方法中是null

更改POST方法签名

[HttpPost] public ActionResult Index(string SearchString) 
{ 
    return RedirectToAction("SearchFromObject", new { searchString = SearchString}); 
} 
+0

它的工作原理!!!!!!感谢名单! – 2015-02-08 13:41:12

0

指定您的post方法的相关数据时,控制器名称和形式操作是这样的:

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

'@using(Html.BeginForm())'会添加默认值,这正是你所做的(假设控制器名称为'Default1Controller'),所以这是不必要的,除非你指定了不同的控制器或动作。 – 2015-02-07 22:46:45