实体“Bond”有一个名为“Kauf”的属性,它是另一个实体(“Price”= date, addedBy, value)。
现在,在创建债券 View (= 购买)时,需要输入价格值。标准创建 View 没有用于输入价格数据的字段。
如果我添加
<div class="editor-field">
@Html.EditorFor(model => model.Kauf.Value)
@Html.ValidationMessageFor(model => model.Kauf.Value)
</div>
到 View ,那么我如何才能在 Controller 中掌握该值,通常只有实体“Bond”被接受为参数?
[HttpPost]
public ActionResult Create(Bond position)
尝试访问它
position.Kauf.Value
我猜只是引用了 bond 的(尚未)空属性“Kauf”。感谢您的输入!
最佳答案
作为答案发布,因为这太长无法发表评论。我已经尝试重新创建并且一切都在这里工作所以我想我会发布我拥有的内容以便您可以与您正在做的事情进行比较:
我假设 Kauf.Value
在这里是一个字符串...
Controller
[HttpGet]
public ActionResult Create()
{
// Setup model before passing in
var model = new Bond();
return View(model);
}
[HttpPost]
public ActionResult Create(Bond position)
{
string theValue = position.Kauf.Value;
// At this point "theValue" contains a valid item
return View(position);
}
查看
@model MvcExperiments.Models.Bond
@using(@Html.BeginForm())
{
<div class="editor-field">
@Html.EditorFor(model => model.Kauf.Value)
@Html.ValidationMessageFor(model => model.Kauf.Value)
</div>
<p>
<input type="submit" value="Save" />
</p>
}
关于c# - MVC 4 - 从 View 传递数据 => Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16817743/