2

I am a young developer in asp.net mvc, now I'm trying to do a simple calculator that calculate the value that user input.

This is my model of 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; }

}

The controller :

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();
    }

The views :

  <% 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(); %>

I want to display the total_money_month from my controller to the in my views, but I have know idea about that. Anyone can tell me please.

1 Answer 1

1

You can pass values to the view by using your ViewBag in your controller:

ViewBag.MonthlyTotal = total_money_month; 
return View();

Then in your view, you can display it:

@ViewBag.MonthlyTotal

Edit:

To use ViewData instead:

Controller:

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

View-

<%= ViewData["MonthlyTotal"]  %>
Sign up to request clarification or add additional context in comments.

3 Comments

Using ViewBag have error : It does not contain in my current context.
Are you using MVC3? If not, you can use ViewData instead.
I want to make my form submit without refresh page, what should I do? I'm pretty new in ajax and jquery.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.