【问题标题】:How to get the data from MVC view to controller如何从 MVC 视图获取数据到控制器
【发布时间】:2017-07-04 00:32:11
【问题描述】:

我正在做一个简单的项目。以下是代码。问题是我无法从视图中获取所有信息到控制器。所以我已经在 Homecontroller 中拥有书籍 ID、名称和价格(来自单独的 Productcontroller),我也可以查看它,并且我想在发出发布请求时发布书籍的数量。因此,当我尝试发出发布请求并对其进行调试时,我只能看到数量,并且我看到图书 ID、名称和价格为空。

我不确定出了什么问题。任何帮助将非常感激。 谢谢!

1.Product.cs

public class Product
    {
        public int ProductID { get; set; }
        public string ProductName { get; set; }
        public decimal ProductPrice { get; set; }
        public int Quantity { get; set; }
    }

2.Cart.cshtml

@model DAL.Models.Product
@{
    ViewBag.Title = "Purchase";
}

@ViewBag.bookName
<h2>Purchase</h2>

<div class="row">
    <div class="col-md-3"></div>
    <div class="col-md-6">
        @using (Html.BeginForm("Cart", "Home", FormMethod.Post))
        {        
            <div class="panel panel-primary">

                <div class="panel-heading">Purchase Book</div>
                <div class="panel-body">
                    <div>
                        <dl class="dl-horizontal">
                            <dt>
                                @Html.DisplayNameFor(model => model.ProductName)
                            </dt>

                            <dd>
                                @Html.DisplayFor(model => model.ProductName)
                            </dd>

                            <dt>
                                @Html.DisplayNameFor(model => model.ProductPrice)
                            </dt>

                            <dd>
                                @Html.DisplayFor(model => model.ProductPrice)
                            </dd>

                        </dl>
                    </div>
                    @Html.LabelFor(model => model.Quantity, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">                      
                        @Html.EditorFor(model => model.Quantity, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.Quantity, "", new { @class = "text-danger" })
                    </div><br />

                    <div class="col-md-10">
                        <input type="submit" value="Submit" class="btn btn-default"/>
                    </div>
                </div>
            </div>
        }
    </div>
    <div class="col-md-3"></div>
</div>

以下是屏幕截图:

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-5


    【解决方案1】:

    只有可编辑的值将包含在 POST 中(您使用 @Html.EditorFor 等)。要在 POST 中包含其他值,只需使用 @Html.HiddenFor() 帮助器。

    例如:

    @Html.HiddenFor(model => model.ProductID)
    @Html.HiddenFor(model => model.ProductName)
    @Html.HiddenFor(model => model.ProductPrice)
    

    这些将被传回并在控制器中可用。

    【讨论】:

    • 太棒了!非常感谢。这行得通。感谢您的帮助。
    【解决方案2】:

    嗨,马歇尔,我希望你从上面的 doug knudsen 答案中得到解决方案,所以我想在这里告诉你方法,Why DisplayFor does not post values to Action Method?

    简短回答:DisplayFor 不会为属性呈现输入字段,而 editorfor 和 hiddenfor 会呈现输入字段。这就是您没有在帖子中获得价值的原因。

    希望它能帮助你了解更多。

    谢谢

    卡提克

    【讨论】:

    • 嗨 Karthik,感谢您花时间查看我的帖子。感谢您帮助我提供详细信息。它有帮助。
    • 我的荣幸 Marshall.Happy 与他人分享知识。:)
    猜你喜欢
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    • 2017-07-24
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    相关资源
    最近更新 更多