Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am getting result from theee tables usign linq to sql but i am unable to use the values in the view, how i can do that as i am new to mvc

here is my controller code:

public ActionResult Grid()
{
    ViewBag.Message = "Your Grid page.";

    bc_limsEntities objbc_limsDBContext = new bc_limsEntities();


    var result = from b in objbc_limsDBContext.dc_tpatient_bookingm
                  join d in objbc_limsDBContext.dc_tpatient_bookingd on b.bookingid equals d.bookingid
                  join t in objbc_limsDBContext.dc_tp_test on d.testid equals t.TestId

                  where b.bookingid == 41239 && d.ProcessID == 0006 && t.SubdepartmentId == 16

                  select new {b.bookingid,
                              d.bookingdid,
                              d.testid,
                              t.Test_Name,
                              t.procedureid,
                              t.ClinicalUse,
                              t.AutomatedText};

    ViewBag.Result = result.ToList();


    return View(result);
}

and I am doing like this in view:

@foreach(var item in ViewBag.Result)
{
    <h1>@item</h1>
}
share|improve this question
Your db context is not getting disposed. Wrap it in a using clause. – asawyer 9 hours ago

3 Answers

Don't use the View bag, use a View Model. You are already passing results as the View Model in this case, so you need to declare the model at the top of your view page, like so:

@model List<Item>

(or whatever type of object results is)

Then you can use it like this:

@foreach(var item in Mode)
{
    <h1>@item.Property</h1>
}

Normally, you would have a dedicated View Model class that contains a number of properties, one of which would be your results list, try that instead if you are feeling adventurous!

EDIT: Because you are dynamically creating a custom object you wont know the type, what you should do is create a class (e.g. Item) that has all the fields you need, and then populate that class from your three tables, then use that.

share|improve this answer
2  
I agree. You should be creating view-model types that can be decorated with annotations and bind your views to those. – asawyer 9 hours ago
by this way my view will be populated but if on click of a particaular item, how i will get the booking id? @asawyer – Ehsan Sajjad 8 hours ago
@EhsanSajjad: If you want to put the booking id somewhere then use @item.bookingid, but this is really a different question – musefan 8 hours ago

How about trying something like:

@foreach(var item in ViewBag.Result)
{
    <h1>@item.bookingid</h1>
}

if of course you wanna stick with using the viewbag but otherwise musefan's suggestion of using a view model is much cleaner and what you should really be doing.

share|improve this answer

I solved my self by this way:

public class ResultSet
{
    public long bookingid { get; set; }
    public long bookingdid { get; set; }
    public long testid { get; set; }
    public string Test_Name { get; set; }
    public long procedureid { get; set; }
    public string ClinicalUse { get; set; }
    public string AutomatedText { get; set; }

}

and in Controller:

bc_limsEntities objbc_limsDBContext = new bc_limsEntities();


        var result = from b in objbc_limsDBContext.dc_tpatient_bookingm
                      join d in objbc_limsDBContext.dc_tpatient_bookingd on b.bookingid equals d.bookingid
                      join t in objbc_limsDBContext.dc_tp_test on d.testid equals t.TestId

                      where b.bookingid == 41239 && d.ProcessID == 0006 && t.SubdepartmentId == 16

                      select new {b.bookingid,
                                  d.bookingdid,
                                  d.testid,
                                  t.Test_Name,
                                  t.procedureid,
                                  t.ClinicalUse,
                                  t.AutomatedText};

        List<ResultSet> resultList = new List<ResultSet>();

        foreach (var temp in result)
        {
            ResultSet objResultSet = new ResultSet();

            objResultSet.bookingid = temp.bookingid;
            objResultSet.bookingdid = temp.bookingdid;
            objResultSet.testid = temp.testid;
            objResultSet.Test_Name = temp.Test_Name;
            objResultSet.procedureid = long.Parse(temp.procedureid.ToString());
            objResultSet.ClinicalUse = temp.ClinicalUse;
            objResultSet.AutomatedText = temp.AutomatedText;

            resultList.Add(objResultSet);
        }

        ViewBag.Result = resultList;
return View(resultList);

and in View like this:

@foreach(var item in Model)
          {
              <tr>
              <td class="t_center"><input type="checkbox" id="c14" name="cc" /><label for="c14"><span></span></label></td>
              <td>@item.bookingid</td>
              <td>@item.bookingdid</td>
              <td>@item.testid</td>
              <td>@item.Test_Name</td>
              <td>@item.procedureid</td>
              <td>@item.ClinicalUse</td>
              <td>@item.AutomatedText</td>

              </tr>                  
          }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.