我尝试在控制器中执行此 *** 作,但这并没有带来结果.我也试过在文本框的值中这样做,这也没有用.
这是我尝试完成此任务的第一种方式
模型
public int? OnHand { get; set; } public int? UnitsOrdered { get; set; }
调节器
VIEwBag.Stock = r.FirstOrDefault().OnHand; VIEwBag.Ordered = r.FirstOrDefault().UnitsOrdered; VIEwBag.Total = VIEwBag.Stock - VIEwBag.Ordered;
视图
<div > @HTML.LabelFor(model => model.UnitsOrdered,(string)"Units On Hand ",HTMLAttributes: new { @class = "control-label col-md-2" }) <div > @HTML.TextBox("OnHand",null,new { @class = "form-control",Value = VIEwBag.Stock,@Readonly = "Readonly" }) </div> </div> <div > @HTML.LabelFor(model => model.UnitsOrdered,(string)"Number of Units Ordered",HTMLAttributes: new { @class = "control-label col-md-2" }) <div > @HTML.EditorFor(model => model.UnitsOrdered,new { HTMLAttributes = new { @class = "form-control",@Readonly = "Readonly" } }) @VIEwBag.MyCountIntro @VIEwBag.MyCount @HTML.ValIDationMessageFor(model => model.UnitsOrdered,"",new { @class = "text-danger" }) </div> </div> <div > @HTML.TextBox("Remaining",Value = VIEwBag.Total,@Readonly = "Readonly" }) </div>
在上面的场景中,VIEwbag.Stock是60.VIEwbag.Ordered是2.VIEwbag.Total应该是58.但相反,它是空白
我也尝试在视图中计算它本身.这也产生了空白.
<div > @HTML.TextBox("Remaining",Value = VIEwBag.Stock - VIEwbag.Ordered,@Readonly = "Readonly" }) </div>解决方法 VIEwBag本身会将值视为对象.在进行计算之前,您应该将该对象强制转换为适当的类型.
VIEwBag.Total =(int)VIEwBag.Stock – (int)VIEwBag.Ordered;
总结以上是内存溢出为你收集整理的c# – 减去两个ViewBag值全部内容,希望文章能够帮你解决c# – 减去两个ViewBag值所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)