my controller function , variable "money"
static int? money = 500; public actionresult submitpurchase(int? orderprice) { money = money - orderprice; viewbag.money = money; if (db.purchases.any()) { db.database.executesqlcommand("delete purchaselists"); return view(); } else return redirecttoaction("order"); } my view
<div align="center"> <p><h1>thank you!</h1></p> <p><h2>your balance @viewbag.money</h2></p> <p><h2>come again plz! :)</h2></p> </div> <a href="/home/order"> <input type="button" value="main menu"/> </a> another view sends url value orderprice in function submitpurchase. problem @viewbag.money in view dosen't shown, should show value of variable money after calculating in function submitpurchase. wrong?
if not passing valid integer value orderprice parameter, code try execute 500-null results in null , null value stored in money variable , set viewbag.
you can null check before doing maths prevent happening.
if(orderprice!=null) money = money - orderprice; the url trying access http://localhost:61314/home/submitpurchase/49 . default mvc route registration, not map orderprice parameter of method. can either update code have url generated like
http://localhost:61314/home/submitpurchase?orderprice=49
or update action method parameter name id
public actionresult submitpurchase(int? id) { if(id!=null) money = money - id; // return view(); } assuming submitpurchaseview (submitpurchase.cshtml) has code read viewbag item , display it.
Comments
Post a Comment