0

In a Asp.net MVC view, i created a form, with a input field.

The user Sets a first name (or part of it), presses the submit button.

This is the form section:

<div> 
    <form action="SearchCustomer" methos="post">
        Enter first name: <input id="Text1" name="txtFirstName" type="text" />
        <br />
        <input id="Submit1" type="submit" value="Search Customer" />
    </form>
</div>

This is the SearchCustomer in the Controller, that gets the data from the form:

CustomerDal dal = new CustomerDal();
string searchValue = Request.Form["txtFirstName"].ToString();
List<Customer> customers = (from x in dal.Customers 
                            where x.FirstName.Contains(searchValue)
                            select x).ToList<Customer>();
CustomerModelView customerModelView = new CustomerModelView();
customerModelView.Customers = customers;

return View("ShowSearch", customerModelView);

When i run the program, and enter a first name ("Jhon" for example), the code returns to SearchCustomer function, but Request.Form is empty.

Why?

Thanks.

4
  • 4
    Typo!..instead of methos="post" it should be method="post" Commented Apr 21, 2016 at 12:01
  • 1
    and there is no action name in form action? Commented Apr 21, 2016 at 12:02
  • 1
    As @KartikeyaKhosla mentioned, you have a typo of 'methos' which should be method, and your action should be in the following format: YourControllerName/YourActionName Commented Apr 21, 2016 at 12:03
  • I fix it to method="post". Thanks. Commented Apr 21, 2016 at 12:11

4 Answers 4

1

Your method is spelled wrongly should not read methos but method like below:

 <form action="SearchCustomer" method="post">
            ....
    </form>
Sign up to request clarification or add additional context in comments.

Comments

1

You need to modify your code:

you need to provide a action name here, which should be defined in your controller(SearchController) with the same name as 'ActionName' you will put in the below code. if SearchController is your action name then provide the controller in which the action is available.

<div> 
    <form action="SearchCustomer/<ActionName>" method="post">
       Enter first name: <input id="Text1" name="txtFirstName" type="text" />
       <br />
       <input id="Submit1" type="submit" value="Search Customer" />
   </form>
</div>

With Html.BeginForm :

  @using (Html.BeginForm("<ActionName>","<ControllerName>", FormMethod.Post))
  {
      Enter first name: <input id="Text1" name="txtFirstName" type="text" />
       <br />
       <input id="Submit1" type="submit" value="Search Customer" />
  }

1 Comment

This approach may well end-up with another 404. Generally, Url.Action() or Html.BeginForm() should be used instead or hard-coding the path.
0

Set [HttpPost] on your controller.

[HttpPost]
 public ActionResult SearchFunction(string txtFirstName)
  {
          CustomerDal dal = new CustomerDal();
         string searchValue = txtFirstName;
         List<Customer> customers = (from x in dal.Customers 
                            where x.FirstName.Contains(searchValue)
                            select x).ToList<Customer>();
         CustomerModelView customerModelView = new CustomerModelView();
        customerModelView.Customers = customers;
       return View("ShowSearch", customerModelView);
  }

1 Comment

my twitter handler : @mohitdagar80
0

If you View is the same name as your ActionResult method, try this:

@using(Html.BeginForm())
{
    ... enter code
}

By default, it'll already be a POST method type and it'll be directed to the ActionResult. One thing to make sure of: You will need the [HttpPost] attribute on your ActionResult method so the form knows where to go:

[HttpPost]
public ActionResult SearchCustomer (FormCollection form)
{
     // Pull from the form collection
     string searchCriteria = Convert.ToString(form["txtFirstName"]);
     // Or pull directly from the HttpRequest
     string searchCriteria = Convert.ToString(Request["txtFirstName"]);
     .. continue code
}

I hope this helps!

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.