开发者

how can print values of "var" initialized in controller?

开发者 https://www.devze.com 2023-03-14 06:16 出处:网络
Controller class: public class PlantHeadController : Controller { private WOMSEntities2 db = new WOMSEntities2();

Controller class:

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;

// please help me in following code in this i want to access code field value of //RawMaterial Table, where 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();

    }
}

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", "I开发者_运维问答ndex")
</div>


The first thing is to pass the model to the view:

return View(res);

Then strongly type the view to this model

@using SomeNamespaceWhereSomeTypeIsDefined
@model IEnumerable<SomeType>

and then you can loop through the values:

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

or even better instead of looping use a display template:

@Html.DisplayForModel()
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号