0

I did a currency converter. but i have a problem. when i click the calculate the page refreshing and the datas which entered by user is cleaning. i think it is because of using return View(); :) .How can i solve this problem? Thanks

{
    ViewBag.Result = frmAmount;
}
return View();

View

</p>
<p>
    <label for="text3">Amount:</label>
    <input type="text" name="frmAmount" />
</p>
<p>
    <input type="submit" value="Calculate" />
</p>
<p>
    <label for="text3">Sonuç:@ViewBag.Result</label>
</p>

For Example the amount be wiped when button clicked

2
  • Use Ajax.BeginFrom instead Html.BeginForm Commented Jun 3, 2014 at 11:47
  • ViewBag would be empty at the time of button click as it stores the value for just one time at the time of redirecting..... Commented Jun 3, 2014 at 12:16

1 Answer 1

2

The amount data is cleaning because when you click on calculate button you are returning the same view again. In order to keep amount data you should also pass amount value to the view by ViewBag (ViewBag.Amount) or by passing object model into view by using second parameter of View(viewName, model).

In general, I would recommend you to try avoid using ViewBag variables. The preffered method is to use ViewModel instead due to e.g. refactoring. You can read more about it here: click!

Sign up to request clarification or add additional context in comments.

Comments

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.