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!