[RESOLVED]How to access a certain model List from a partial VIEW

Hi,

I have defined a model that contains within it, 2 models as such :

    public class TBRModel
    {
        public List<TBModel> TBModel { get; set; }
        public List<TRModel> TRModel { get; set; }
    }

My controller adds to the TBModel and TRModel and then at the end adds to the TBRModel. When i do this in debug mode i see that this is happens correctly.

        [HttpPost]
        public ActionResult OTBHeaderTR(string tID, string taUserName, DateTime tbDate)
        {
            bool exec_sp_success = false;

            ViewBag.userName = SessionBag.Current.UserName;
            ViewBag.userTypeID = SessionBag.Current.userTypeID;

            TempData["frompage"] = TempData["l_frompage"];

            DBController dbcontroller1 = new DBController();

            if (dbcontroller1.DBConnection())
            {
                MySqlCommand command1 = new MySqlCommand("view_tr_header;", dbcontroller1.conn);
                command1.CommandType = System.Data.CommandType.StoredProcedure;

                MySqlCommand command2 = new MySqlCommand("view_user_rate_detail;", dbcontroller1.conn);
                command2.CommandType = System.Data.CommandType.StoredProcedure;

                MySqlCommand command3 = new MySqlCommand("view_tr_min_max_avg;", dbcontroller1.conn);
                command3.CommandType = System.Data.CommandType.StoredProcedure;

                ......................
                .....................

                try
                {
                    MySqlDataReader rdr1 = command1.ExecuteReader();

                    var tbList = new List<TBModel>();

                    var trB = new TBModel();

                    while (rdr1.Read())
                    {
                        trB.tID = (long)(rdr1["tID"]);
                        ...............
                        ...............
                        tbList.Add(trB);
                    }

                    //Close the first SP Call
                    rdr1.Close();
                    exec_sp_success = true;

                    MySqlDataReader rdr2 = command2.ExecuteReader();

                    var tRateList = new List<TRModel>();
                    while (rdr2.Read())
                    {
                        var tComments = new TRModel();
                        tComments.comments = rdr2["vComments"].ToString();
                        tComments.userRate = (Int32)(rdr2["userRate"]);
                        tRateList.Add(tComments);
                    }

                    rdr2.Close();

                    if (exec_sp_success == true)
                    {
                        MySqlDataReader rdr3 = command3.ExecuteReader();

                        while (rdr3.Read())
                        {
                            trB.tMin = (double)(rdr3["trMinB"]);
                            ........
                            .........
                            tbList.Add(trB);
                        }

                        var model = new TBRModel();

                        model.TBModel = tbList;
                        model.TRModel = tRateList;

                        rdr3.Close();
                        dbcontroller1.conn.Close();

                        ViewBag.userName = SessionBag.Current.UserName;
                        TempData["userTypeID"] = SessionBag.Current.userTypeID;
                        return PartialView("_OtherTBDetailTR", model);
                    }
                    else
                    {
                        dbcontroller1.conn.Close();

                        ViewBag.Message = "An Error has occurred. Could not view detail. Please try again or contact the system administrator";
                        return PartialView("_TRError");
                    }
                }
                catch (MySql.Data.MySqlClient.MySqlException ex)
                {
                    dbcontroller1.conn.Close();

                    ViewBag.Message = "Could not view detail. Error " + ex.Number + " has ocurred. Please try again or contact the system administrator.";
                    return PartialView("_TRError");
                }
            }
            else
            {
                ViewBag.Message = "Could not connect to the database. Please try again or contact the system administrator";
                return PartialView("_TRError");
            }
        }

I see the TBRModel contains the 2 models TBModel and TRModel .. By hovering the mouse over TBModel (within TBRModel) i can see 2 TBModels {0} and {1} .. now in my PartialVIEW im trying to do a foreach statement but obviously it is spooling out {0} and {1}
of the TBModels each time im doing a foreach. What i want is to do a foreach on {0} model and then do a separate foreach on {1} TBModel .. I hope this si clear ..

A snippet of my PartialView is :

How do i do this ?

@model grepTravelMVC.Models.TBRModel
      
      @foreach (var item in Model.TBModel)
        {
            <div align="right">
                <font color="#000000" size="2"><b>Total:</b></font>
                <font color="#000000" size="2">@Html.DisplayFor(modelItem => item.Count) |</font>

                <font color="#000000" size="2"><b>Min:</b></font>
                <font color="#000000" size="2">@Html.DisplayFor(modelItem => item.Min) |</font>

                <font color="#000000" size="2"><b>Max:</b></font>
                <font color="#000000" size="2">@Html.DisplayFor(modelItem => item.Max)</font>
            </div>
        }

Thanks Kindly

Naren

I read your post three times and still couldn’t get it.

Try this, move all of your SQL to a Service layer.  Create a folder named Services and put the SQL logic there.  Then for your view create a strongly typed view that has both of the model objects in them before the view is displayed.  

Then tell us what happens…

Hi,

Thanks for the response. For the time being, if you could assist me with the way it is currently structured.

I have created 2 models, TBModel and TRModel and each have their owen set of variables defined. The 3rd model i created has just these 2 models in there. The reason is that i want to be able to use 2 models in my PartialView.

    public class TBRModel
    {
        public List<TBModel> TBModel { get; set; }
        public List<TRModel> TRModel { get; set; }
    }

So, I build my TBModel by calling a stored procedure and return the resultSet of the stored procedure into my Model and add it to a list as such:

tbList.Add(trB)

The above works fine and i do see it being created.

My 2nd stored procedure, returns the resultSet to the first Model and adds to the list of that Model as such:

tbList.Add(trB);

 

I then call another final (3rd) stored procedure, but this time the resultSet goes into another Model and i add it to a new list as such:

tRateList.Add(tComments);

 

I then add these 2 built lists to the 3rd Model i had defined cause the 3rd model contains the other 2, as per below ..

                        var model = new TBRModel();

                        model.TBModel = tbList;
                        model.TRModel = tRateList;

Now, the model.TBModel contains 2 defined Model Structures .. and i can see that during debug mode. One shows TBModel{0} and the other TBModel{1} .. the {0} structure has the returned values of the 1st stored procedure and the {1} contains the resultSet of
the 2nd stored procedure.

So, how do i access the individual {0} and {1} of TBModel ? I want to outout this in my Partial VIEW as defined in my first post. Sorry, i dont know how else to explain this for im not 100% familiar with the .Net terminology. The {0} and {1} is what i see
when im in debug mode, and i got no other way of explaining this.

The data is correct, and im happy with what im returning into the model, however, i just want to be able to access this model that looks like its an array .. ?

Please can you assist

Thanks

Naren

 

If anyone wants to know as to how to combine models into 1 and then use it in a Partial View, this is what ive done. This is a workaround, however, im not sure what the performance impact would be though, if any ..

I have now created 3 models :

  public class TBModel
    {
       public Int64 tID { get; set; }
       ...
    }

    public class TRateModel
    {
	[Display(Name = "User Name")]
          public string taUserName { get; set; }
    }
  
    public class TMMModel
    {
        [Display(Name = "tMB")]
        public double tMB { get; set; }
    }
    
    public class TBRateModel
    {
        public List<TBModel> TBModel { get; set; }
        public List<TRateModel> TRateModel { get; set; }
        public List<TMMModel> TMMModel { get; set; }
    }

I then added the 3 models into 1 model called TBRateModel

In my controller, i created a "LIST" for each model and called my stored procedure which adds to the individual fields in each model.

I then add each list to the TBRateModel as such:

                       var model = new TBRateModel();

                        model.TBModel = trBList;
                        model.TRateModel = trRateList;
                        model.TMMModel = trMMList;

I then just call my Partial View by passing in "model" as a parameter:

return PartialView("_OtherTBDetailTR", model);

In my PartialView I perform the below to access my 3 individual models:

                        @model gTMVC.Models.TBRateModel
                        ....
                        
                        @foreach (var item in Model.TMMModel)
                        {
                           @Html.DisplayFor(modelItem => item.tID)
                           ....
                           ....
                        }
                        
                        @foreach (var item in Model.TBModel)
                        {
                           @Html.DisplayFor(modelItem => item.taUserName)
                           ....
                           ....
                        }

 

Leave a Reply