how can i have my page inherit from a class instead of a table?
i need to access data from a table and from a view on a particular page, so 开发者_开发百科i made a class that can access them both. looks like this `
namespace LoanManager.Models
{
public class Loan_vwFieldValues
{
public Loan loan { get; set; }
public VW_FIELD_VALUE vwFieldValues { get; set; }
}
}
at the top of my page i have this
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" Inherits="System.Web.Mvc.ViewPage<IEnumerable<LoanManager.Models.Loan_vwFieldValues>>" %>
then i have this my controller after some other other code that works.
return View(_db.LOAN_VWFIELDVALESs.Where(predicate));
and my definition for that looks like this.
public System.Data.Linq.Table<Loan_vwFieldValues> LOAN_VWFIELDVALESs
{
get
{
return this.GetTable<Loan_vwFieldValues>();
}
}
this works fine for all the rest of my pages (they have different names of course), but they are not a class, they just reference one table or a view i have made, but this page needs to access a table and a view, but it complains when it gets to this last part of posted code(return statement) and says that Loan_vwFieldValues is not mapped as a table, which is true because it is NOT a table, i also have this to fill the Loan_vwFieldValues
public ActionResult LoanProperties(int id)
{
Loan_vwFieldValues l = new Loan_vwFieldValues();
l.loan = (from a in _db.Loans where a.LOAN_ID == id select a).First();
l.vwFieldValues = (from v in _db.VW_FIELD_VALUEs where v.Loan_ID == id select v).First();
return View(l);
}
but how can i get this to work with my class? thank you in advance.
You need to create a collection of Loan_vwFieldValues and return that. We don't have enough information to tell you how to construct those objects from your data access layer.
In other words...
return View(_db.LOAN_VWFIELDVALESs.Where(predicate).Select(
x => new Loan_vwFieldValues()
{
loan = yourCodeHere,
vwFieldValues = moreOfYourCodeHere
}));
thanks every one that helped, i found the solution by doing the following... i hope it can help others as well.
IQueryable<Loan> loanList = _db.Loans.Where(predicate);
List<Loan_vwFieldValues> loanVwList = new List<Loan_vwFieldValues>();
foreach (Loan loan in loanList)
{
Loan_vwFieldValues loanVw = new Loan_vwFieldValues();
loanVw.loan = loan;
loanVw.vwFieldValues = (from n in _db.VW_FIELD_VALUEs where n.Loan_ID == loan.LOAN_ID select n).First();
loanVwList.Add(loanVw);
}
return View(loanVwList);
精彩评论