0

My requirement as follows, In my database, have tables as follows.In there table called Tbl_loan_details LOANID reference to other tables as FK(las_fac_id and lac_fac_id).

enter image description here

I need to get all the 3 tables columns to my view called details.

This is my model classes,

public partial class Tbl_Loan_Details
{
    public int LOANID { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string CUSAGE { get; set; }
    public string CUSNAME { get; set; }
    public string AMOUNT { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
}

public partial class Tbl_CRIB_Details
{
    public int CRIBID { get; set; }
    public Nullable<int> LAS_FAC_ID { get; set; }
    public string CRIB_NAME { get; set; }
    public string CUSAGE { get; set; }
    public string CUSNAME { get; set; }
    public string CHECQ { get; set; }

    public virtual Tbl_Loan_Details Tbl_Loan_Details { get; set; }
}

public partial class Tbl_loan_para
{
    public int ID { get; set; }
    public string paraName { get; set; }
    public string Amount { get; set; }
    public Nullable<int> lac_fac_id { get; set; }

    public virtual Tbl_Loan_Details Tbl_Loan_Details { get; set; }
}

my controller as follows, currently which accept parameter as id, and return only tbl_loan_details

// GET: /Loan/Details/5
public ActionResult Details(int? id)
{

    Tbl_Loan_Details tbl_loan_details = db.Tbl_Loan_Details.Find(id);
    return View(tbl_loan_details);
}

view Detail code as follows.

<h2>Details</h2>
<div>
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.LastName)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.LastName)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.FirstName)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.FirstName)
        </dd>
    </dl>
</div>
<p>
    @Html.ActionLink("Edit", "Edit", new { id = Model.LOANID }) |
</p>

How can I access other table's column in detail view. I mean I need to show following tbl_loan_para(paraName) Tbl_crib_detail(Crbi_name,Checq) data in details view.

I pass id to my controller as above code, based on that id I need to show each table data in details view.

thomsan
  • 433
  • 5
  • 19
  • 1
    Use a view model for your view. – Shyju Sep 04 '18 at 17:03
  • @DavidG Please don't mark it as duplicate, I tried that as well, but I'm confused to use it to my solution . so please give me sample code – thomsan Sep 04 '18 at 17:08
  • @Shyju I'm not clear, please give me sample code – thomsan Sep 04 '18 at 17:08
  • See the duplicate tagged to the question. The answer in that question is exactly what you have to do in your case. – Shyju Sep 04 '18 at 17:09
  • So, how can I pass parameter id? – thomsan Sep 04 '18 at 17:12
  • Parameter is of your action method. So it stays as it is. Think about your view model as a cart you use to carry data from your grocery shop to your car. it is basically a structure to move data(multiple items). You are responsible for loading it ( populating the property values) and unloading (rendering the values in views) it. so read the entity object using the id and map the value to the viewmodel – Shyju Sep 04 '18 at 17:31

0 Answers0