如何打印控制器中初始化的“ var”值?

| 控制器类:
public class PlantHeadController : Controller
{
    private WOMSEntities2 db = new WOMSEntities2();
    //
    // GET: /PlantHead/

    public ActionResult Index()
    {
        ViewBag.productCode = new SelectList(db.Product, \"ID\",\"code\");


        return View();
    }
    public ActionResult printReceipt(PlantHeadInfo plantHeadInfo)
    {
        ViewBag.name = plantHeadInfo.productManagerName;
        ViewBag.pCode = plantHeadInfo.productCode;
        int p = plantHeadInfo.productCode;
        ViewBag.WareHouseDescription = new SelectList(db.WareHouse, \"Description\", \"Description\");
       /// ViewBag.WareHouseDescription = plantHeadInfo.WareHouseDescription;
        ViewBag.amount = plantHeadInfo.amount;

        ViewBag.minQuantity = db.ProductFormulation.Where(r => r.ProductID == 1);
      ViewBag.pc = db.Product.Where(r => r.ID == p).Single().Code;


      var results = db.ProductFormulation.Where(r => r.ProductID == p);


      ViewBag.rawMaterial = db.ProductFormulation.Where(r => r.ProductID == 1);

        ViewBag.chetan=db.Product;
//请在以下代码中帮助我,我想访问// RawMaterial表的代码字段值,其中product表中的ProductID = 1。
         ViewBag.res = (from x in db.RawMaterial join y in db.ProductFormulation on x.ID equals y.RawMaterialID where y.ProductID == 1 select y.Code);

        return View();

    }
}
视图
@model IEnumerable<CalcoWOMS.Models.ProductFormulation>
@{
    ViewBag.Title = \"Generate Receipt\";
    Layout = \"~/Views/Shared/_firstLayout.cshtml\";
}

<h2>Index</h2>


@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend></legend>

        <div class=\"editor-label\">
            @Html.Label(\"Product Manager\")
        </div>
        <div class=\"editor-field\">

            @ViewBag.name           
        </div>

        <div class=\"editor-label\">
            @Html.Label(\"productCode\")
        </div>
        <div class=\"editor-field\">
            @ViewBag.pCode

        </div>


         <div class=\"editor-label\">
            @Html.Label(\"WareHouse\")
        </div>
        <div class=\"editor-field\">
            @Html.DropDownList(\"WareHouseDescription\", \"SELECT\")

        </div>

        <div class=\"editor-label\">
            @Html.Label(\"Quantity\")
        </div>
        <div class=\"editor-field\">
            (@ViewBag.amount)

        </div>


        <div class=\"editor-label\">
            @Html.Label(\"Checking\")
        </div>
        <div class=\"editor-field\">
            (@ViewBag.pc)

        </div>

            <div>
            hello
        @foreach(var album in @ViewBag.rawMaterial)
         {
                <h2>@album.RawMaterialID</h2>
         }
         </div>



         <div>
            <!-- help me here what should i write here below code to use res which is sent from controller-->

        @foreach(var album in ViewBag.res)
         {
                <h2>@album</h2>
         }
         </div>




        <p>
            <input type=\"submit\" value=\"Print Receipt\" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink(\"Back to List\", \"Index\")
</div>
    
已邀请:
        首先是将模型传递给视图:
return View(res);
然后强烈键入该模型的视图
@using SomeNamespaceWhereSomeTypeIsDefined
@model IEnumerable<SomeType>
然后可以遍历以下值:
@foreach (SomeType item in Model)
{
    ...
}
甚至更好,而不是循环使用显示模板:
@Html.DisplayForModel()
    

要回复问题请先登录注册