【发布时间】:2010-04-08 18:26:25
【问题描述】:
我在将数据从文本框获取到控制器时遇到了困难。我在 Sanderson 的书 Pro ASP.NET MVC Framework 中阅读了一些实现此目的的方法,但没有取得任何成功。
另外,我在网上遇到了一些类似的问题,但也没有成功。好像我错过了一些相当基本的东西。
目前,我正在尝试使用操作方法参数方法。有人可以指出我哪里出错或提供一个简单的例子吗?提前致谢!
使用 Visual Studio 2008、ASP.NET MVC2 和 C#: 我想做的是获取在“调查员”文本框中输入的数据,并使用它来过滤控制器中的调查员。我计划在 List 方法中执行此操作(它已经可以使用),但是,我正在使用 SearchResults 方法进行调试。
这是我认为的文本框代码,SearchDetails:
<h2>Search Details</h2>
<% using (Html.BeginForm()) { %>
<fieldset>
<%= Html.ValidationSummary() %>
<h4>Investigator</h4>
<p>
<%=Html.TextBox("Investigator")%>
<%= Html.ActionLink("Search", "SearchResults")%>
</p>
</fieldset>
<% } %>
这是来自我的控制器 InvestigatorsController 的代码:
private IInvestigatorsRepository investigatorsRepository;
public InvestigatorsController(IInvestigatorsRepository investigatorsRepository)
{
//IoC:
this.investigatorsRepository = investigatorsRepository;
}
public ActionResult List()
{
return View(investigatorsRepository.Investigators.ToList());
}
public ActionResult SearchDetails()
{
return View();
}
public ActionResult SearchResults(SearchCriteria search)
{
string test = search.Investigator;
return View();
}
我有一个 Investigator 类:
[Table(Name = "INVESTIGATOR")]
public class Investigator
{
[Column(IsPrimaryKey = true, IsDbGenerated = false, AutoSync=AutoSync.OnInsert)]
public string INVESTID { get; set; }
[Column] public string INVEST_FNAME { get; set; }
[Column] public string INVEST_MNAME { get; set; }
[Column] public string INVEST_LNAME { get; set; }
}
并创建了一个 SearchCriteria 类,看看我是否可以让 MVC 将搜索条件数据推送给它并在控制器中抓取它:
public class SearchCriteria
{
public string Investigator { get; set; }
}
}
我也不确定项目布局是否与此有关,但我使用的是 Sanderson 建议的 3 个项目方法:DomainModel、Tests 和 WebUI。 Investigator 和 SearcCriteria 类在 DomainModel 项目中,这里提到的其他项目在 WebUI 项目中。
再次感谢您提供任何提示、提示或简单示例!
迈克
【问题讨论】:
标签: asp.net-mvc-2