1

I have a View with several form that I'm using for searching and displaying the results as partial View in like SearchByNumber, SearchByVehicle, etc.

I'm trying to load view and execute search for different forms by posting link with querystring like www.example.com/Search?number=101010 from different view.

For the first form, SearchByNumber I only have one parameter, string number and i'm returning view with dynamic Model and its working like it should, but I only manage to make search for this form.

Here is my controller:

public ActionResult Index(string number)
{
  return View(model: number);
}

and in the View I have:

<form id="searchbynumberform">
    Search By Any Number:
    <div class="input-group input-group-sm">
        <input type="text" class="form-control" name="number" id="number" value="@Model">
        <span class="input-group-btn">
            <button class="btn btn-primary" type="button" name="numbersearch" id="numbersearch" disabled>
                Search
            </button>
        </span>
    </div>
</form>

My Question is, if anyone can help me, How to perform search let's say on the second form where I have int type and string name parameters?

Thank You in advance...

4
  • what exactly is confusing you about implementing the second search type? Sounds like you just need another action method which accepts those different parameters. Commented Oct 23, 2019 at 13:05
  • @ADyson It's confusing, how to assign multiple parameters to dynamic model? Commented Oct 23, 2019 at 13:35
  • it's not clear what you mean by "dynamic" exactly? As I understand it, you have two different forms with two different sets of search parameters. So you just need two action methods which accept different parameters. They can both run the same sort of thing underneath and even return the same view. Commented Oct 23, 2019 at 14:07
  • Alternatively if you want both forms to post to the same action, then you could have a viewmodel which is an object (i.e. based on a class you define) containing all the potential search properties. Of course each form will only submit some of them, so you'd then need to check which properties had values and perform your search on that basis. Commented Oct 23, 2019 at 14:15

1 Answer 1

2

At the moment your Model is only the search string that was entered, which seems rather incomplete. It would make a lot more sense if the Model also contained the actual search results, which after all is what the user wants to see. And then you can also add the other search properties.

The MVC approach for this is to create a (View)Model class, somewhere in your project, something like this:

public class SearchModel
{
    public string Number { get; set; }
    public int? Type { get; set; }
    public string Name { get; set; }
    public List<SearchResult> SearchResults { get; set; }
}

And then use it e.g. like this:

public ActionResult Index(string number)
{ 
    var model = new SearchModel
    {
        Number = number,
        SearchResults = GetByNumber(number)
    };
    return View(model);
}

public ActionResult IndexOther(int type, int name)
{
    var model = new SearchModel
    {
        Type = type,
        Name = name,
        SearchResults = GetByTypeAndName(type, name)
    };
    return View(model);
}

And in your Index.cshtml:

@model SearchModel

@* You can now use Model.Number, Model.Type, Model.Name and Model.SearchResults. *@
Sign up to request clarification or add additional context in comments.

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.