views:

738

answers:

4

Hi,

I'm having difficulty getting data from a textbox into a Controller. I've read about a few ways to accomplish this in Sanderson's book, Pro ASP.NET MVC Framework, but haven't had any success.

Also, I've ran across a few similiar questions online, but haven't had any success there either. Seems like I'm missing something rather fundamental.

Currently, I'm trying to use the action method parameters approach. Can someone point out where I'm going wrong or provide a simple example? Thanks in advance!

Using Visual Studio 2008, ASP.NET MVC2 and C#: What I would like to do is take the data entered in the "Investigator" textbox and use it to filter investigators in the controller. I plan on doing this in the List method (which is already functional), however, I'm using the SearchResults method for debugging.

Here's the textbox code from my view, SearchDetails:

    <h2>Search Details</h2>

 <% using (Html.BeginForm()) { %> 
 <fieldset>
     <%= Html.ValidationSummary() %>
     <h4>Investigator</h4>
     <p>
        <%=Html.TextBox("Investigator")%>
        <%= Html.ActionLink("Search", "SearchResults")%>
     </p>
  </fieldset>
 <% } %>

Here is the code from my controller, 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();
    }

I have an Investigator class:

    [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; }
}

and created a SearchCriteria class to see if I could get MVC to push the search criteria data to it and grab it in the controller:

    public class SearchCriteria
{
    public string Investigator { get; set; }
}

}

I'm not sure if project layout has anything to do with this either, but I'm using the 3 project approach suggested by Sanderson: DomainModel, Tests, and WebUI. The Investigator and SearcCriteria classes are in the DomainModel project and the other items mentioned here are in the WebUI project.

Thanks again for any hints, tips, or simple examples!

Mike

A: 

try strongly typing the page to use SearchCriteria to autopost the data like that ex:

public partial class Search: ViewPage<SearchDetails>
Russell Steen
+1  A: 

This should do it for you (unable to verify this is perfect - typed this from memory):

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SearchDetails(FormCollection formValues)
{
    var txtContents = formValues["Investigator"];

    // do stuff with txtContents

    return View();
}
Jaxidian
+1  A: 

Hi Mike,

1.) Have you looked into ViewModels for your View? In essence that is what your SearchCriteria class is. Make sure you strongly type your view with that model:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/MyMaster.Master" Inherits="System.Web.Mvc.ViewPage<SearchCritieria>"
  1. Also make sure that you use the HtmlHelper.TextBoxFor method to map this Investigator property to the SearchCritiera model. On Post back your text box value should be there:

    '<%=Html.TextBoxFor(model => model.Invesigator)%>'

Good luck!

Also here is a great reference on using ViewModels that I have looked at a lot recently:

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

Daryl
A: 

Thanks for the tips everyone. For learning purposes, I need to go back and follow the strongly typed route. I'm curious if I would have run into this problem if I would have done that from the beginning.

Until then, the following worked:

  1. Use a submit button
  2. Use this code for the form:

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

Thanks again for you help!

Mike

mr_plumley