从MVC控制器到View的数据传输

时间:2013-09-23 20:32:27

标签: jquery asp.net-mvc-4 twitter-bootstrap knockout.js

在我的视图中,从控制器到表格的最佳方法是什么?

CONTROLLER

     [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

提前谢谢!

1 个答案:

答案 0 :(得分: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来访问返回到视图的数据。