简体   繁体   English

数据没有从 View 发送到 Controller?

[英]Data is not send from View to Controller?

I'm trying to send an object from a razor view to a controller method in ASP.NET MVC 5.The issue is that the object is not sent to the controller.This is my View: I'm trying to send an object from a razor view to a controller method in ASP.NET MVC 5.The issue is that the object is not sent to the controller.This is my View:

 @using Models;
@{ 
    var ListedCompanies = ViewBag.ListedCompanies as IEnumerable<Company>;
}
@model Models.Individual

<head>

</head>

<body>     
    <div style="width:70% ; float :left">
        <table id="verticalScroll" class="table table-striped table-bordered table-sm" cellspacing="0">
            <thead class="thead-dark">
                <tr>
                    <th>
                        CompanyName
                    </th>
                    <th>
                        SharePrice
                    </th>
                    <th>
                        SharesCount
                    </th>
                    <th>
                        SharesOnTheMarket
                    </th>
                    <th>
                        Details
                    </th>

                </tr>
            </thead>
            <tbody>

                @foreach (Company o in ListedCompanies)
                {
                    <tr>
                        <td>
                            @o.CompanyName
                        </td>
                        <td>
                            @o.SharePrice
                        </td>
                        <td>
                            @o.SharesCount
                        </td>
                        <td>
                            @o.SharesOnTheMarket
                        </td>
                        <td>

                            <button type="button" value="Details" class="btn btn-primary"
                                    onclick="location.href='@Url.Action("Transaction","User", new Transaction { Company = o,Individual = (Individual)Model})'">
                                Details
                            </button>

                        </td>

                    </tr>
                }

            </tbody>
        </table>
    </div>

</body>

And this is the controller method:这是 controller 方法:

    public ActionResult Transaction(Transaction Transaction)
    {
        return RedirectToAction("CompanyDetails", Transaction);
    }

This is the Transaction object:这是事务 object:

   public class Transaction
{
    public ASK ASK { get; set; }
    public BID BID { get; set; }
    public Individual Individual { get; set; }
    public Company Company { get; set; }

}

The issue is that when I click the button,the proprieties of the Transaction object from the controller method are all null.问题是当我点击按钮时,controller方法中的事务object的属性都是null。 I tried using [HTTPPost] tag,and also to use a form instead of a button,like this:我尝试使用 [HTTPPost] 标签,也尝试使用表单而不是按钮,如下所示:

 @using (Html.BeginForm("Transaction", "User", FormMethod.Post, new Transaction {Company = o,Individual = (Individual)Model}))
   {
      <input type="hidden" name="Transaction" />
      <input type="submit" value="Transaction" class="btn btn-primary active" />
   }

Also,did not worked.I also tried to add the method from controller in RouteConfig and I have the same result.另外,没有用。我还尝试在 RouteConfig 中添加 controller 的方法,结果相同。

You're trying to serialize an entire complex object here:您正在尝试在此处序列化整个复杂的 object:

onclick="location.href='@Url.Action("Transaction","User", new Transaction { Company = o,Individual = (Individual)Model})'"

From a comment on the question above, it's just writing the string representation of each property:根据对上述问题的评论,它只是编写每个属性的字符串表示形式:

location.href='/User/Transaction?Individual=System.Data.Entity.DynamicProxies.Individual_38FCAB5A9CFA20BF79D8EDBDD997A0CA040A96BD2C33DAB33CB4A343BE8BDF7B&Company=System.Data.Entity.DynamicProxies.Company_5BF87B6217AD9FE8C136ECD77D64A8AE5DC9669791BB1FEA512FD500FE1CCFE4'

Take a step back... Why do you need the entire object here?退一步……为什么这里需要整个 object Consider it conceptually for a moment.从概念上考虑一下。 The server is building this whole object, sending this whole object to the client, only to have the client send this whole object unmodified by the user back to the server.服务器正在构建整个 object,将整个 object 发送给客户端,只是让客户端将未经用户修改的整个 object 发送回服务器。 (Or at least the intent is for the user to not modify it. What happens when the user does modify it? Will the server trust this entire object when it posts back?) (或者至少目的是让用户修改它。当用户修改它时会发生什么?服务器在回发时会信任整个 object 吗?)

Ultimately... Why?最终……为什么? There's no need to send the entire object to the client just to send the entire object back to the server.无需将整个 object 发送给客户端,只需将整个 object 发送回服务器即可。 All the server needs is enough information to re-fetch the object from data.服务器所需要的只是足够的信息来从数据中重新获取 object。 An identifier of some kind.某种标识符。 In a database this is usually a primary key field.在数据库中,这通常是主键字段。

(Note: The user can still modify the identifier. Make sure you validate that the information coming from the client is valid for the current logged-in user.) (注意:用户仍然可以修改标识符。确保验证来自客户端的信息对当前登录用户有效。)

So what identifiers do you need?那么你需要什么标识符呢? It looks like you are only looking for the Company and the Individual .看起来您只是在寻找CompanyIndividual Let's assume for the sake of example that they both have a property called ID which is a single value to identify those records in your data.让我们假设它们都有一个名为ID的属性,它是用于标识数据中这些记录的单个值。 Then all you need are those identifiers:那么你所需要的就是这些标识符:

onclick="location.href='@Url.Action("Transaction","User", new { companyID = o.ID, individualID = ((Individual)Model).ID }'"

This would render a link more like:这将使链接更像:

location.href='/User/Transaction?companyID=123&individualID=234'

In the server-side action you would then expect just those values:在服务器端操作中,您将只期望这些值:

public ActionResult Transaction(int companyID, int individualID)
{
    // Fetch the records to build your Transaction object
}

As an aside, it's also worth noting that your action currently re-serializes the whole object again and sends it back to the client again in the form of a redirect.顺便说一句,还值得注意的是,您的操作当前再次重新序列化整个 object 并以重定向的形式再次将其发送回客户端。 It looks like there are several levels of indirection happening here, and you're trying to pass an entire object back and forth between the server and the client multiple times.看起来这里发生了多个间接级别,并且您试图在服务器和客户端之间多次来回传递整个 object。 There's definitely no need for that.绝对没有必要这样做。

Keep the data between the server and client minimal, only pass around the values that are necessary.保持服务器和客户端之间的数据最少,只传递必要的值。 The server can re-construct objects from data faster and more reliably than the network can transmit the entire objects multiple times, and the code will be simpler in doing so.与网络多次传输整个对象相比,服务器可以更快、更可靠地从数据中重建对象,并且这样做的代码会更简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM