I have an Edit View which is a form. I would like the user to be able to lookup/search for a UPC (search against a database table) and see the matching records on that form, so they can select a result, which would then populate a field on the Edit View form, and get submitted to POST with the form.
The piece I'm working on is the lookup/search. I have the search box on my Edit View form, and an empty div for the search results:
<div class="form-group" id="search-pac">
@Html.Action("PacSearch", "ItemRequest");
</div>
<div class="form-group" id="search-pac-results">
</div>
On submit, I am successfully seeing the UPC hit the controller:
[HttpPost]
public ActionResult PacSearch(string pacupc)
{
if (pacupc != null)
{
try
{
List<PriceAssociationLookup> matchingPacs = new List<PriceAssociationLookup>();
matchingPacs = matchingPacs.GetPacs(pacupc);
return PartialView("_PacSearchResultsPartial", matchingPacs);
}
catch (Exception e)
{
Alert.SetAlert(this.HttpContext, String.Format("There was an error in the Price Association Code lookup for UPC {0}. Error: {1}", pacupc, e), "alert-warning");
}
}
return PartialView("_PacSearchResultsPartial","UPC not found");
}
But I'm having trouble with what I should do in the controller in order to get the list of matching records from the database. With the code I currently have in the controller, I get the error: "Error 1 'System.Collections.Generic.List' does not contain a definition for 'GetPacs' and no extension method 'GetPacs' accepting a first argument of type 'System.Collections.Generic.List' could be found (are you missing a using directive or an assembly reference?)"
GetPacs is defined to accept a string. How do I pass the pacups string to GetPacs?
Here is the related class:
namespace Portal.Repository.SqlDatabase {
public partial class PriceAssociationLookup : IPriceAssociationLookupRepository
{
#region IPriceAssociationLookupRepository Members
IEnumerable<IPriceAssociationLookupRepository> IPriceAssociationLookupRepository.GetPacs(string upc)
{
using (PortalDataEntities entities = new PortalDataEntities())
{
var priceAssociationLookups = (from priceassociationlookup in entities.PriceAssociationLookups
where priceassociationlookup.Upc == this.Upc
select priceassociationlookup).ToList();
return priceAssociationLookups;
}
}
#endregion IPriceAssociationLookupRepository Members
My first recommendation would be to try removing .ToList since you are returning only an IEnumerable:
#region IPriceAssociationLookupRepository Members
IEnumerable<IPriceAssociationLookupRepository> IPriceAssociationLookupRepository.GetPacs(string upc)
{
using (PortalDataEntities entities = new PortalDataEntities())
{
var priceAssociationLookups = (from priceassociationlookup in entities.PriceAssociationLookups
where priceassociationlookup.Upc == this.Upc
select priceassociationlookup);
return priceAssociationLookups;
}
}
You may get a new error in which your context has expired - but let me know.
Here is the answer: Controller:
[HttpPost]
public ActionResult PacSearch(string pacupc)
{
if (pacupc != null)
{
try
{
PriceAssociationLookup pacRep = new PriceAssociationLookup();
//List<PriceAssociationLookup> matchingPacs = pacRep.GetPacs(pacupc);
return PartialView("_PacSearchResultsPartial", pacRep.GetPacs(pacupc));
}
catch (Exception e)
{
Alert.SetAlert(this.HttpContext, String.Format("There was an error in the Price Association Code lookup for UPC {0}. Error: {1}", pacupc, e), "alert-warning");
}
}
return PartialView("_PacSearchResultsPartial","UPC not found");
}
and the partial view being returned needs to be typed as:
@model List<Portal.BusinessModel.Entities.PriceAssociationLookup>
I think I may still have a problem with the return from the controller when a UPC match is not found (return PartialView("_PacSearchResultsPartial","UPC not found"); ), but the main question I originally asked has been solved with a change to the controller and partial view.
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.