1

我可以将以下数据从我的控制器获取到我视图中的表格的最佳方法是什么。

控制器

     [HttpPost]
     public ActionResult PBHEP(string PC,string FC)
     {
        JavaScriptSerializer ser = new JavaScriptSerializer();
        List<Conditions> PC_rows = ser.Deserialize<List<Conditions>>(PC);
        List<Conditions> FC_rows = ser.Deserialize<List<Conditions>>(FC);


        PlotXYContext plotXYContext = new PlotXYContext();
        int Township = PC_rows[0].Township, Range = PC_rows[0].Range, Section =  PC_rows[0].Section, Acres = PC_rows[0].Acres, Crop = Convert.ToInt32(PC_rows[0].Crop);

        var query_XYCells = from TRS2Cell in plotXYContext.XYCells
                            where (TRS2Cell.Twnshp == Township) && (TRS2Cell.Rng == Range) && (TRS2Cell.Section == Section)
                            select new { TRS2Cell.Xcell, TRS2Cell.Ycell };
        var output_XYCells = query_XYCells.ToList();

        int XCell = Convert.ToInt32(output_XYCells[0].Xcell), YCell = Convert.ToInt32(output_XYCells[0].Ycell);
        var query_CIRvalues = from aAvgCIR in plotXYContext.CIRvalues
                              where (aAvgCIR.X == XCell) && (aAvgCIR.Y == YCell) && (aAvgCIR.Crop == Crop)
                              select new { aAvgCIR.PumpNetIrrig, aAvgCIR.NonPumpNetIrrig, aAvgCIR.PumpDeepPerc, aAvgCIR.NonPumpDeepPerc };
        var output_CIRvalues = query_CIRvalues.ToList();

        float CIR = output_CIRvalues[0].PumpNetIrrig + output_CIRvalues[0].NonPumpNetIrrig,
            Recharge = output_CIRvalues[0].PumpDeepPerc + output_CIRvalues[0].NonPumpDeepPerc,
            GW_Withdrawal = (CIR - Recharge) * (Acres / 12);

        ViewBag.CIR = CIR;
        return View();
    }

    public ActionResult PBHEP()
    {
        return View();
    }

看法

    <tbody>
        <tr>
            <td style =" text-align:center" colspan="10"><b>Computed CIR Offsets</b></td>
        </tr>
        <tr>
            <td style =" text-align:center"><b>Township</b></td>
            <td style =" text-align:center"><b>Range</b></td>
            <td style =" text-align:center"><b>Section</b></td>
            <td style =" text-align:center"><b>Crop</b></td>
            <td style =" text-align:center"><b>Acres</b></td>
            <td style =" text-align:center"><b>CIR(IN)</b></td>
            <td style =" text-align:center"><b>Recharge(IN)</b></td>
            <td style =" text-align:center"><b>GW Withdrawal(AF)</b></td>
            <td style =" text-align:center"><b>Platte River Depletion (Percent)</b></td>
            <td style =" text-align:center"><b>Estimated Net Depletion (AF)</b></td>
        </tr>
        <tr>
            <td style =" text-align:center" colspan="10"><b>Present Conditions</b></td>
        </tr>
        @(ViewBag.CIR > 0)
        {
         <tr>
            <td style =" text-align:center"><b></b></td>
            <td style =" text-align:center"><b></b></td>
            <td style =" text-align:center"><b></b></td>
            <td style =" text-align:center"><b></b></td>
            <td style =" text-align:center"><b></b></td>
            <td style =" text-align:center"><b></b></td>
            <td style =" text-align:center"><b></b></td>
            <td style =" text-align:center"><b></b></td>
            <td style =" text-align:center"><b></b></td>
            <td style =" text-align:center"><b></b></td>
        </tr>
        }
    </tbody>

我的 VIEW 中有上表,并且希望在控制器中填充浮点值的下一行我如何将它们放在那里。最佳做法是什么?

我尝试使用 ViewBag 更新一个值,但无法更新我的 UI

先感谢您!

4

1 回答 1

2

您可以将强类型视图与自定义类的模型一起使用,例如,采用以下控制器:

public class HomeController : Controller
{
    public ActionResult TaskView()
    {
        Task t = new Task() { Id = "1", Description= "Task 1", Name="Some Task Name"};

        return View(t);
    }
}

public class Task
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string  Description { get; set; }
}

那么您的视图可能如下所示:

@model MvcApplication1.Controllers.Task

@{
    ViewBag.Title = "TaskView";
}

<h2>TaskView</h2>
@Model.Id - @Model.Name - <b>@Model.Description</b>

请注意我如何@model在页面的开头使用来定义数据类型,@Model然后在页面中访问返回给视图的数据。

于 2013-09-23T21:26:55.527 回答