Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在asp.net mvc中将结果从控制器显示到视图#_C#_Jquery_Asp.net_Asp.net Mvc - Fatal编程技术网

C# 在asp.net mvc中将结果从控制器显示到视图#

C# 在asp.net mvc中将结果从控制器显示到视图#,c#,jquery,asp.net,asp.net-mvc,C#,Jquery,Asp.net,Asp.net Mvc,我是asp.NETMVC的一名年轻开发人员,现在我正在尝试做一个简单的计算器来计算用户输入的值 这是我的mvc模型: public class MoneyInstallments { [Required(ErrorMessage="The price is required.")] public double price { get; set; } [Required(ErrorMessage = "The duration is required.")] pu

我是asp.NETMVC的一名年轻开发人员,现在我正在尝试做一个简单的计算器来计算用户输入的值

这是我的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(); %>

货币信息
产品价格:
:
m、 价格,新的{id=“price”,name=“price”})%>
m、 价格)%>
每月利息
:
2.9%
期间
:
m、 持续时间,新的{id=“持续时间”,name=“持续时间”})%>
m、 持续时间)%>
月薪
:

我想在我的视图中显示从我的控制器到的每月总金额,但我知道这一点。任何人都可以告诉我。

您可以使用控制器中的ViewBag将值传递给视图:

ViewBag.MonthlyTotal = total_money_month; 
return View();
 ViewData["MonthlyTotal"] = total_money_month; 
 return View();
然后在您的视图中,您可以显示它:

@ViewBag.MonthlyTotal
编辑:

要改用ViewData,请执行以下操作:

控制器:

ViewBag.MonthlyTotal = total_money_month; 
return View();
 ViewData["MonthlyTotal"] = total_money_month; 
 return View();
看法-



使用ViewBag时出错:它不包含在我当前的上下文中。是否使用MVC3?如果没有,您可以改用ViewData。我想在不刷新页面的情况下提交表单,我该怎么办?我对ajax和jquery相当陌生。