【问题标题】:ASP.NET MVC2: Getting textbox data from a view to a controllerASP.NET MVC2:将文本框数据从视图获取到控制器
【发布时间】: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


    【解决方案1】:

    尝试强烈键入页面以使用 SearchCriteria 自动发布数据,例如:

    public partial class Search: ViewPage<SearchDetails>
    

    【讨论】:

      【解决方案2】:

      这应该为您完成(无法验证这是完美的 - 从记忆中输入):

      [AcceptVerbs(HttpVerbs.Post)]
      public ActionResult SearchDetails(FormCollection formValues)
      {
          var txtContents = formValues["Investigator"];
      
          // do stuff with txtContents
      
          return View();
      }
      

      【讨论】:

        【解决方案3】:

        1.) 您是否为您的视图查看过视图模型?本质上,这就是您的 SearchCriteria 类。确保使用该模型强烈键入视图:

        <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/MyMaster.Master" Inherits="System.Web.Mvc.ViewPage<SearchCritieria>"
        
        1. 还要确保使用 HtmlHelper.TextBoxFor 方法将此 Investigator 属性映射到 SearchCritiera 模型。在 Post back 你的文本框值应该在那里:

          ' model.Invesigator)%>'

        祝你好运!

        这里还有一个很好的关于使用 ViewModels 的参考资料,我最近看了很多:

        http://geekswithblogs.net/michelotti/archive/2009/10/25/asp.net-mvc-view-model-patterns.aspx

        【讨论】:

          【解决方案4】:

          感谢大家的提示。出于学习目的,我需要返回并遵循强类型路线。如果我从一开始就这样做,我很好奇我是否会遇到这个问题。

          在此之前,以下工作:

          1. 使用提交按钮
          2. 将此代码用于表单:

            <% using(Html.BeginForm(new { Action = "SearchResults"})) { %> <% } >
            

          再次感谢您的帮助!

          迈克

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-05-16
            相关资源
            最近更新 更多