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

时间:2011-06-18 08:49:48

标签: asp.net-mvc asp.net-mvc-2 asp.net-mvc-3

控制器类:

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表的代码字段值,其中ProductID = 1 in product table。

         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>

1 个答案:

答案 0 :(得分:1)

首先要将模型传递给视图:

return View(res);

然后强烈输入此模型的视图

@using SomeNamespaceWhereSomeTypeIsDefined
@model IEnumerable<SomeType>

然后你可以遍历值:

@foreach (SomeType item in Model)
{
    ...
}

甚至更好,而不是循环使用显示模板:

@Html.DisplayForModel()