سلام من دو عدد رو نمی تونم در mvc جمع کنم.
این کلاس ViewModel:
public class sumViewModel { public int number1 { get; set; } public int number2 { get; set; } public int res { get; set; } }
این کنترلر:
public ActionResult Index() { return View(); } [HttpPost] public ActionResult Index(sumViewModel smviewModel) { if (ModelState.IsValid) { smviewModel.res = smviewModel.number1 + smviewModel.number2; } return View(smviewModel); }
view هم ساختم
ولی توی خروجی Nan نشون میده و جواب رو نمیاره
در ضمن من توی ViewModel خاصیت Requaierd نگذاشتم ولی چرا به خالی بودنش گیر میده؟؟؟؟؟!!!!!!1
کد View را قرار دهید
@model sum.Models.sumViewModel @{ ViewBag.Title = "Index"; } <h2>Index</h2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>sumViewModel</h4> <hr /> @Html.ValidationSummary(true) <div class="form-group"> @Html.LabelFor(model => model.number1, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.number1) @Html.ValidationMessageFor(model => model.number1) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.number2, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.number2) @Html.ValidationMessageFor(model => model.number2) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.res, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.res) @Html.ValidationMessageFor(model => model.res) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" class="btn btn-default" /> </div> </div> </div> } <div> @Html.ActionLink("Back to List", "Index") </div> @section Scripts { @Scripts.Render("~/bundles/jqueryval") }
کدتون به نظر هیچ مشکلی نداره
ببخشید میشه برام یه نمونه بگذارید؟
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)