Vikas
Vikas

Reputation: 97

how to return two variables from controller to view in mvc3

Hi Im a new to MVC3 and i am having problem in returning two variables here is my controller code

public ActionResult Create()
    {
        using (var context = new material_managementEntities())
        {
            var blogs = (from cs in context.GROUP_MASTER
                         where cs.GROUP_NAME == "MIC"
                         select cs.ID).FirstOrDefault();

            var droplist = new TRANSACTION();
            droplist.GROUP_MASTER_ID = blogs;
            droplist.GROUP_NAME = "MIC";
            var directory_master = db.DIRECTORY_MASTER.Include(d => d.CATEGORY_MASTER).Include(d => d.REPRESENTATIVE_MASTER);
            droplist.dir_data = directory_master.ToList();
            return View(droplist);
        }

    }

here Transaction is one table and DIRECTORY_MASTER is another and GROUP_MASTER_ID is a foreign key to Transaction table, what i want is the toList data will be displayed in a modalpopup box so i need to pass data available from both tables

here is model

 namespace Material.Models
{
using System;
using System.Collections.Generic;
using System.Web.Mvc;
public partial class TRANSACTION
{
    public int ID { get; set; }
    public int GROUP_MASTER_ID { get; set; }
    public string GROUP_NAME { get; set; }
    public string dir_data { get; set; }
    public string NAME { get; set; }
    public string ADDRESS_DETAILS { get; set; }
    public string CONTACT_PERSON { get; set; }
    public Nullable<int> TEL_NO { get; set; }
    public Nullable<int> CELL { get; set; }
    public Nullable<int> FAX { get; set; }
    public string EMAIL_ID { get; set; }
    public Nullable<int> OPENING_BALANCE { get; set; }
    public Nullable<System.DateTime> OPENING_BALANCE_DATE { get; set; }
    public string VERIFIED { get; set; }

    public virtual GROUP_MASTER GROUP_MASTER { get; set; }
}

}

Upvotes: 1

Views: 3227

Answers (2)

sudil ravindran pk
sudil ravindran pk

Reputation: 3126

Either you can use a

viewbag.directory_master = (db.DIRECTORY_MASTER.Include(d => d.CATEGORY_MASTER).Include(d => d.REPRESENTATIVE_MASTER)).ToList();

and then iterate your viewbag.directory_master from your model and display the required ones.

or you can create a custom view model containing directory_master and TRANSACTION like

public class DashboardModel
{
 public TRANSACTION transation{get;set;}
 public List<directory_master>directoryMaster{get;set;}
}

so your action will be like this

public ActionResult Create()
    {
        using (var context = new material_managementEntities())
        {
            var blogId = (from cs in context.GROUP_MASTER
                         where cs.GROUP_NAME == "MIC"
                         select cs.ID).FirstOrDefault();

            DashboardModel dashboardModel= new DashboardModel();
            dashboardModel.transation.GROUP_MASTER_ID = blogId;
            dashboardModel.transation.GROUP_NAME = "MIC";
            dashboardModel.directoryMaster = (db.DIRECTORY_MASTER.Include(d => d.CATEGORY_MASTER).Include(d => d.REPRESENTATIVE_MASTER)).ToList();           
            return View(dashboardModel);
        }

    }

Upvotes: 2

Maritim
Maritim

Reputation: 2159

I would recommend strongly typing your MVC view to take a model of the type you require, and pass that model to the view via an argument in the return function of your controller's action method.

Upvotes: 1

Related Questions