我有一个从模型脚手架的视图。我需要从“editfor”帮助程序标记中检索值,执行一堆计算,然后将结果(多个)传回给视图。我创建了一个小例子来澄清。将表单内容传递给控制器,进行计算,然后传回多个结果
public class OpticalcTestViewModel
{
public double OD_Sphere { get; set; }
public double OD_Cylinder { get; set; }
public int Axis { get; set; }
}
它创建这个支架:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>OpticalcTestViewModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.OD_Sphere, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OD_Sphere, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.OD_Sphere, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.OD_Cylinder, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OD_Cylinder, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.OD_Cylinder, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Axis, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Axis, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Axis, "", new { @class = "text-danger" })
</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")
}
这是我的控制器:
public class OpticalcTestController : Controller
{
// GET: OpticalcTest
public ActionResult Index()
{
return View();
}
}
我如何走出 “editfor” 框中的值,对其进行计算,然后将这些计算结果(多个结果,而不仅仅是一个)传递回视图中的某些标签?
在winforms中这是一件很简单的事情,这正是我通常使用的,但我正在努力寻找答案。看起来我从搜索中得到的其他9,000个结果总是关于将数据(作为整体模型)写入数据库。这将没有数据库。这只是一个采用数值的形式,计算和吐出结果。如何将这些值拉入控制器,将球体添加到圆柱体,然后将圆柱体添加到轴并将两个结果分别传回到标签(或以其他方式查看它们)??????????????????????????
感谢, [R
请注意,在JavaScript中进行数学计算是完全可行的,因此您不一定必须将值发送回服务器以对其运行计算。除非你有某些特定的原因(比如隐藏你的计算结果)将它们放在服务器上。 – NightOwl888
我想过这样做,但是会有很多计算,我需要很好的调试和编写测试用例的能力。 – rmnrdi
如果您确实需要在服务器上执行此操作,那么您应该考虑使用ajax发布值并使用返回的值更新DOM以提高性能 –