views:

34

answers:

2

I have a simple form based off a model called Visitor. I'd like to have a search button by one of the id text fields so the user can click the button and have the page populate with visitor information: first name, last name, etc. In Web Forms I would do something like this:

page_load(){
    person = businessManager.FindPersonById(Convert.ToInt32(txtId.Text));
    txtFirstName.Text = person.FirstName;
    txtLastName.Text = person.LastName;
     ...
}

Prior to the search button, my view form called SignIn worked just fine; posted the data to the controller and did its thing:

    [HttpPost]
    public ActionResult SignIn(Visitor visitor) {
        if (ModelState.IsValid) {
            visitorRepoistory.Add(visitor);
            visitorRepoistory.Save();
            return RedirectToAction("/");
        } else {
            return View(new VisitorFormViewModel(visitor));
        }
    }

But now that I have a search button placed on my view form, I'm totally lost. I don't know how to wire the search button to the controller so I can: 1.) Look up the data and 2.) Return it back to the sign in form to populate the fields. What are the steps I need to take to accomplish this?

Thanks.

A: 

Take a look here:

http://stackoverflow.com/questions/2444245/handling-2-buttons-submit-actions-in-a-single-view-form-asp-net-mvc-2-rtm

Leniel Macaferi
Problem is, I need to return back to the same page and populate the fields with the search results. I think that's where I'm getting confused.
Beavis
Do the search and return to the view passing to it your result.
Leniel Macaferi
A: 

This question has been duplicated many times on SO

http://stackoverflow.com/questions/822751/multiple-forms-in-asp-net-mvc

But to answer your question you can have multiple forms on one page and have different Actions handle the submits. That's what the link above outlines.

Specific to your case:

View

<% Html.BeginForm("Search", "<ControllerName>"); %>
    Your search controls here
<% Html.EndForm(); %>

<% Html.BeginForm("SignIn", "<ControllerName>"); %>
    Your signin controls here
<% Html.EndForm(); %>

Controller

[HttpPost]
public ActionResult Search(FormCollection collection)
{
   Do your search and return a view
}

[HttpPost]
public ActionResult SignIn(Visitor visitor) 
{
    if (ModelState.IsValid) {
        visitorRepoistory.Add(visitor);
        visitorRepoistory.Save();
        return RedirectToAction("/");
    } else {
        return View(new VisitorFormViewModel(visitor));
    }
}
Scott Lance
This is a single form. The search is just a way to look up the visitor information and populate the fields within the same form.
Beavis
Right but you added a new button that does a submit, since you can't technically submit multiple actions on a single form you need to have a form for each action (e.g. Search and SignIn functionality) you wish to have.
Scott Lance
You can submit multiple actions in a single form. See my answer.
Leniel Macaferi
If you are passing the same strongly typed data in both actions then yes you can have a single controller action for multiple submits (e.g. both submits use a Visitor object). But if you are passing different data for each action then a strongly typed action won't work (e.g. Search takes 4 fields not related to visitor, and signin takes a visitor). You would have to parse the entire form collection for both types of actions. If that's the case multiple forms is a better way to go IMO
Scott Lance