0

I want to make search function through the link.

Currently, I have search box in home page. It is working the search box.

However, I don't know how to pass the some parameters to Search method in controller.

When I put like this coding, searchString is 'null' in controller.

How can I get serachString parameter through actionLink?

Could you help me? Maybe it looks easy. Please help me or advice me. Thanks.

//mac.cshtml

<h3>CPU Processor</h3>
<ul>
    <li>@Html.ActionLink("Intel Core i5", "Search", "Store", new { @searchString = "i5"})</li>
    <li>@Html.ActionLink("Intel Core i7", "Search", "Store", new { @searchString = "i7" })</li>
</ul>

//Search method in StoreController
public ActionResult Search(string searchString)
    {

        var product = from a in _db.Product.Include(a => a.Category)
                      select a;
        if (!String.IsNullOrEmpty(searchString))
        {
            product = product.Where(a => a.model.ToUpper().Contains(searchString.ToUpper())
                               || a.Category.name.ToUpper().Contains(searchString.ToUpper()));
        }
        return View(product.ToList());
    }
wholee1
  • 1,491
  • 13
  • 34
  • 51

1 Answers1

1

You don't use the right overload for Html.ActionLink

see

http://msdn.microsoft.com/en-us/library/system.web.mvc.html.linkextensions.actionlink.aspx

you should do

@Html.ActionLink("Intel Core i7", "Search", "Store", new { @searchString = "i7" }, null)

it's often confusing with "object" as parameters.

By the way, this method is dangerous (get parameters), and the ToUpper method won't work if you are in linq2entities

Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122