Homam
Homam

Reputation: 23841

Failed to pass routeValues to an action from partial view

I wanna add search functionality in my MVC application. This functionality should be available for all pages. So, I added it into the shared layout.

The only problem I have is that I'm not able to pass the routValues to the Search action in spite of I'm creating a new Form inside the partial view.

Target action:(performs the search)

[HttpPost]
public ActionResult Search(SearchModel keyword)
{
    // keyword is always null
    return RedirectToAction("SearchResult", keyword.keyword);
}

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

The Partial View:

@model DocuLine.Models.SearchModel

@using (Html.BeginForm("Search", "Home", FormMethod.Post))
{
    @Html.EditorFor(model => model.Keyword)
    <input type="submit" value="Search" />
}

Upvotes: 1

Views: 416

Answers (3)

Softlion
Softlion

Reputation: 12595

Try

public ActionResult Search(SearchModel model)

Upvotes: 2

Homam
Homam

Reputation: 23841

Finally, I solve it. The problem is that the SearchModel parameter name is keyword and it should be anything except keyword becuase there's already an html control is rendered with this name.

To solve it, it should only be named with another name:

public ActionResult Search(SearchModel model)
{
     // model now has a value.
}

Upvotes: 0

andres descalzo
andres descalzo

Reputation: 14967

Why the class is defined as Keyword and then view SearchResult you pass it as a keyword (lower case first letter)?

1 UpdateModel

You can update the class SearchModel with the method UpdateModel to check if you can make the correct assignment.

2 FormCollection

You can try to receive as parameter in the method Search form data (FormCollection) and check if you get what you require.

Upvotes: 0

Related Questions