2013-03-14 88 views
3

我正在尝试构建用last姓名搜索我的Employees的simnple搜索实用程序。ASP.NET MVC 4:表单输入值在提交时始终为空

这里是我的Razor视图

@using(Html.BeginForm("Index","Employee", FormMethod.Post)) 
{ 
    <p> 
     Search employees by Last Name : @Html.TextBox("SearchString") 
     <input type="submit" value="Submit" name="Search" />  
    </p> 
} 

这里是我的控制器

 // GET: /Employee/ 
    public ActionResult Index(string lastName) 
    { 
     var employees = db.Employees; 
     if (!String.IsNullOrEmpty(lastName)) 
     { 
      employees = employees.Where(p => p.LastName.ToUpper().Contains(lastName.ToUpper())); 
     }    
     return View(employees.ToList()); 
    } 

调试显示提交按钮回发到索引方法,但lastName的返回到索引方法的价值始终是空值。我怎样才能正确地传递姓氏?

+0

谢谢,这里的例子:http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with实体框架在一个ASP网络mvc应用程序结果是不正确的然后。 – 2013-03-14 18:02:51

回答

3

@Html.TextBox("SearchString")名称和操作方法的参数变量名字必须匹配。 (SearchString在)

[HttpPost] 
public ActionResult Index(string SearchString) 
{ 
    var employees = db.Employees; 
    if (!String.IsNullOrEmpty(SearchString)) 
    { 
     employees = employees.Where(p => p.LastName.ToUpper().Contains(SearchString.ToUpper())); 
    }    
    return View(employees.ToList()); 
} 
0

所以在你的榜样,无论是TextBox值设置为lastName您必须命名您的ActionResult一样的字段名的变量或名称的ActionResult IndexSearchString