0

At the @OBJECT I am trying to pass the contents of the search in string form to the Search Controller. My question is how to grab the contents of Textbox from the parent or if there is a better way to approach this problem without using a button or exterior link, thanks in advance.

<form class="searchBarForm" method="post" 
 action="@Url.Action("Search", "Search", @OBJECT)>
        @Html.TextBox("value", null, 
        new { @id = 'search_content'});
</form>

Update

the @Url.Action(VIEW, CONTROLLER, @OBJECT), the @OBJECT is the routeValue to the controller and for my application I just need it to be a string that gets its value from the content of the textbox.

I removed excess code for readability.

3
  • I can't understand what you are asking about. Commented Feb 23, 2014 at 5:38
  • the @Url.Action(VIEW, CONTROLLER, @OBJECT), the @OBJECT is the routeValue to the controller and for my application I just need it to be a string that gets its value from the content of the textbox. Commented Feb 23, 2014 at 5:42
  • @Url.Action is calculated at runtime on the SERVER, you cannot get the content value of the textbox and use razor to update the url. you would have to use javascript Commented Feb 23, 2014 at 10:24

3 Answers 3

1

You are doing it wrong. In MVC, you do not need to manually do such things. The form values – in this case, the contents of the textbox – will be posted automatically, and if properly bound to a model, your controller action will receive them.

E.g.:

public class SearchModel
{
    public string Query { get; set; }
}

public class SearchController
{
    [HttpPost]
    public ActionResult Search(SearchModel data)
    {
        ...
    }
}

@model SearchModel
@using(Html.BeginForm("Search", "Search", FormMethod.Post))
{
    @:Html.EditorFor(model => model.Query);
}
Sign up to request clarification or add additional context in comments.

Comments

0

you need to do it it this way

@Url.Action("Search", "Search", new {routeParameter = routeValue})

1 Comment

you must already know what the value is that you are passing it.
0

The above answer works but I ended up with a different solution. Was initially unaware of the FormCollection object and how to access the variables based off name.

// The controller
[HttpPost]
public ActionResult Search(FormCollection collection)
{
    string searchQuery = collection["searchQuery"];
    ... do those swweet sweet calcs ...
}

// The view
<form class="searchBarForm" method="post" [email protected]("Search", "Search")>
      @Html.TextBox("searchQuery", null, new { @placeholder = "Search . . .", @class = "form-control searchBar", @id = "search_contents"})
</form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.