c# - MVC 4 - 从 View 传递数据 => Controller

标签 c# asp.net-mvc

实体“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/

相关文章:

c# - 将 MFC COM 服务器移植到托管代码的最佳方式

c# - TraceSource 和 ASP.NET : Works for Web Applications, 不是网站

c# - "Cannot insert the value NULL into column "但我的专栏是 Nullable ... Entity Framework 5 asp.net MVC

jQuery 向 Controller 提交表单及其模型

c# 控制 ip 网络摄像头

C#工厂模式和IoC的区别

c# - Observable.Interval 同时执行一个方法两次

asp.net-mvc - 在我自己的 OWIN 中间件中使用 Ninject DI

css - RAZOR 中的标签重载无效

asp.net-mvc - 是否有 MicrosoftMvcAjax 客户端库引用/文档?