简体   繁体   中英

Searching Multiple Fields in ASP.net MVC: Passing parameters from view to controller

This is in relation to post Filter/Search using Multiple Fields - ASP.NET MVC . I am new to ASP and MVC logic, so I am not quite sure how to actually assign the values that are typed in the input box to values in the model (basically pass the search parameters from view to controller, where I am referencing ProductSearchModel as an argument). I am familiar with ViewData, however, in my index page, I am using the Product model, not the ProductSearchModel. I could not comment under the thread but I really hope someone can help with this. Thank you so much.

EDIT: So just as described in the referenced post above, I have a DonationSearchModel class, a DonationSearchLogic class, and a DonationView class.

In my DonationController:

 public ActionResult Index(DonationSearchModel searchModel)
         {
            
             var donations = from m in _context.Donation
                            .Include(p => p.Person)
                            .AsNoTracking()
                             select m;

            
            var dv = new DonationView();

            var search = new DonationSearchLogic(_context);            
            var model = search.GetDonations(searchModel);
            dv.Donations = model;

            return View(dv);
            //return View(await donations.ToListAsync());
         }

In my Index.cshtml:

@model TissueBankApp.Models.View.DonationView
<form Donation asp-action="Index" method="get">
    <p>
        @using (Html.BeginForm("Index", "DonationController", FormMethod.Get))
        {
            <label class="control-label">Forename</label>
            @Html.TextBoxFor(m => m.DonationSearchModel.Forename)

            <label class="control-label">Surname</label>
            @Html.TextBoxFor(m => m.DonationSearchModel.Surname)

            <label class="control-label">DOB</label>
            @Html.TextBoxFor(m => m.DonationSearchModel.DOB)

            <input type="submit" class="btn btn-outline-primary btn-sm" value="Search" />
        }
    </p>
</form>

My DonationView:

namespace TissueBankApp.Models.View
{
    public class DonationView
        {
            //
            // TODO: This was thrown together to get the associated view working. It needs to be replaced with a proper viewmodel containing all required attributes, and related methods to map those attributes to the data models.
            //
    
            public Donation Donation { get; set; }
            public Person Person { get; set; }
            public Contact PlaceOfDeathContact { get; set; } 
            public Contact ConsentContact { get; set; }
            
            public Contact FuneralDirectorContact { get; set; }
            public DonationSearchModel DonationSearchModel { get; set; }
            public string CaseType { get; set; }
    
            public IEnumerable<Donation> Donations { get; set; }
    
    
        }

}

At the moment, the display is fine, but when I actually search (for example search "Anna" in the forename field) and debug the Index function, the forename property is null, so it just displays everything since there are no searched-for strings. It seems like the search string is not being passed from view to controller and I cannot figure out why. I hope this gives you more context!

According to your comment, i want to share solution for your problem. Of course you can change get or post method depends on you, but you are right about being get method. Filtering methods are generally http get. There are two approach for get filtered value from view to controller: One way;

    [HttpGet]
    public ActionResult Index(DonationView viewModel)
     {
        DonationSearchModel search= viewModel.DonationSearchModel;
        //You can reach your search model over donationview
       
        return View();
        //return View(await donations.ToListAsync());
     }

Second way;

    [HttpGet]
    public ActionResult Index([Bind(Prefix = "DonationSearchModel")]DonationSearchModel donationSearchModel)
     {
        DonationSearchModel search= donationSearchModel;
        //You can reach your search model over donationSearchModel
       
        return View();
        //return View(await donations.ToListAsync());
     }

You can create a search filter class in order to use in view part. For example;

public class Product 
{
    public int Id { get; set; }

    public double Price { get; set; }

    public string Name{ get; set; }
 }

public class ProductSearchModel 
{
    public double MinPrice{ get; set; }

    public double MaxPrice{ get; set; }

    public string SearchName{ get; set; }
 }


[HttpPost]
public ActionResult Index(ProductSearchModel model)
{
    if (ModelState.IsValid)
    {
        //TODO: Your service method with viewmodel, then send value to view             
    }

    return View();
}

And you can use them in view part with html helpers on mvc to send or get value between controller-view.

@model Project.ProductSearchModel 
@using (Html.BeginForm("Index", "Home", FormMethod.Post))  
{  
<br />  

   @Html.LabelFor(m => m.MinPrice) 
   @Html.TextBoxFor(m => m.MinPrice)   
<br />  

   @Html.LabelFor(m => m.MaxPrice) 
   @Html.TextBoxFor(m => m.MaxPrice)    
<br />  
   @Html.LabelFor(m => m.SearchName) 
   @Html.TextBoxFor(m => m.SearchName) 
<br />  
<input type="submit" class="btn btn-outline-primary" value="Send" />

} 

Above codes are very basic usage. If you can explain more detail what you do in your project, I can help you more. Also you have net core version in your project, you can use tag helpers effectively.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM