簡體   English   中英

在ASP.NET MVC C#中從控制器向視圖顯示結果

[英]Display result from controller to the view in asp.net mvc c#

我是asp.net mvc的年輕開發人員,現在我正在嘗試做一個簡單的計算器來計算用戶輸入的值。

這是我的mvc模型:

public class MoneyInstallments
{
    [Required(ErrorMessage="The price is required.")]
    public double price { get; set; }

    [Required(ErrorMessage = "The duration is required.")]
    public int duration { get; set; }

    public double interestRate = 0.029;
    public double total_interest { get; set; }
    public double total_money { get; set; }
    public double total_money_month { get; set; }

}

控制器:

public ActionResult Index()
{
 return View();
}
[HttpPost]
public ActionResult Index(FormCollection frm)
{
        string price_str = frm["price"].ToString();
        string duration_str = frm["duration"].ToString();
        double price;
        double duration;

        try
        {
            price = Convert.ToDouble(price_str);
            duration = Convert.ToDouble(duration_str);
            double interestRate = 0.029;
            double total_interest = interestRate * duration;
            double total_money = total_interest + price;
            double total_money_month = total_money / duration;

        }
        catch (FormatException)
        {
            Response.Write("Unable to caculate"+price_str);
        }
        catch (OverflowException)
        {
            Response.Write("Unable to caculate" + price_str);
        }


        return View();
    }

觀點 :

  <% Html.BeginForm(); %>
    <fieldset>
        <legend>Money Information</legend>
    <table>
        <tr>
            <td>Product Price:</td>
            <td>:</td>
            <td>
                <%: Html.TextBoxFor(m => m.price, new { id = "price", name = "price"})%> <!-- user can type only 50 char of the string -->
                <%: Html.ValidationMessageFor(m => m.price)%>
            </td>
        </tr>
        <tr>
            <td>Interest per month</td>
            <td>:</td>
            <td>2.9%</td>
        </tr>
        <tr>
            <td>Duration</td>
            <td>:</td>
            <td>
                <%: Html.TextBoxFor(m => m.duration, new {id="duration", name="duration"}) %>
                <%: Html.ValidationMessageFor(m => m.duration) %>
            </td>
        </tr>
        <tr>
            <td colspan="3" align="right"><input type="submit" value="Calculate"id="btnCalculate"/></td>
        </tr>
        <tr>
            <td>Monthly Pay is</td>
            <td>:</td>
            <td><div id="result"></div></td>
        </tr>
    </table>
    </fieldset>
    <% Html.EndForm(); %>

我想在我的視圖中顯示從控制器到的total_money_month,但是我對此有所了解。 任何人都可以告訴我。

您可以通過在控制器中使用ViewBag將值傳遞給視圖:

ViewBag.MonthlyTotal = total_money_month; 
return View();

然后,在您的視圖中,可以顯示它:

@ViewBag.MonthlyTotal

編輯:

要改用ViewData:

控制器:

 ViewData["MonthlyTotal"] = total_money_month; 
 return View();

視圖-

<%= ViewData["MonthlyTotal"]  %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM