MVC 4 ViewModel 没有被发送回控制器 [英] MVC 4 ViewModel not being sent back to Controller

查看:29
本文介绍了MVC 4 ViewModel 没有被发送回控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法弄清楚如何将整个 ViewModel 发送回控制器的验证和保存"功能.

I can't seem to figure out how to send back the entire ViewModel to the controller to the 'Validate and Save' function.

这是我的控制器:

[HttpPost]
public ActionResult Send(BitcoinTransactionViewModel transaction)
{
}

这是视图中的表单:

<li class="check">
    <h3>Transaction Id</h3>
     <p>@Html.DisplayFor(m => m.Transaction.TransactionId)</p>
</li>
<li class="money">
    <h3>Deposited Amount</h3>
    <p>@Model.Transaction.Amount.ToString()  BTC</p>
</li>
<li class="time">
    <h3>Time</h3>
    <p>@Model.Transaction.Time.ToString()</p>
</li>


@using (Html.BeginForm("Send", "DepositDetails", FormMethod.Post, new { transaction = Model }))
{

@Html.HiddenFor(m => m.Token);
@Html.HiddenFor(m => m.Transaction.TransactionId);

    @Html.TextBoxFor(m => m.WalletAddress, new { placeholder = "Wallet Address", maxlength = "34" })
    <input type="submit" value="Send" />    

    @Html.ValidationMessage("walletAddress", new { @class = "validation" })
}

当我点击提交时,控制器包含 walletAddress 字段的正确值,但 transaction.Transaction.Timetransaction.Transaction.Locationtransaction.Transaction.TransactionId 为空.

When i click on submit, the conroller contains the correct value of the walletAddress field but transaction.Transaction.Time, transaction.Transaction.Location, transaction.Transaction.TransactionId are empty.

有没有办法将整个模型传回控制器?

Is there a way i could pass the entire Model back to the controller?

当我什至没有在控制器中收到 walletAddress 时.一切都归零了!当我单独删除这一行时:@Html.HiddenFor(m => m.Transaction.TransactionId);它可以工作并且我获得了控制器上的 Token 属性,但是当我将它添加回来时,控制器上 transaction 对象的所有属性都是 NULL.

When i dont even receive the walletAddress in the controller. Everything gets nulled! When i remove this line alone: @Html.HiddenFor(m => m.Transaction.TransactionId); it works and i get the Token property on the controller, but when i add it back, all the properties of the transaction object on the controller are NULL.

这是 BitcoinTransactionViewModel:

Here is the BitcoinTransactionViewModel:

public class BitcoinTransactionViewModel
    {
        public string Token { get; set; }
        public string WalletAddress { get; set; }
        public BitcoinTransaction Transaction { get; set; }
    }

public class BitcoinTransaction
    {
        public int Id { get; set; }
        public BitcoinTransactionStatusTypes Status { get; set; }
        public int TransactionId { get; set; }
        public decimal Amount { get; set; }
        public DateTime Time { get; set; }
        public string Location { get; set; }
    }

有什么想法吗?

我想通了,它在下面的标记答案中......

推荐答案

好的,我一直在做其他事情,但又一次遇到了同样的问题.只是这一次我想出了如何让它发挥作用!

OK, I've been working on something else and bumpend into the same issue all over again. Only this time I figured out how to make it work!

以下是任何可能感兴趣的人的答案:

显然,有一个命名约定.注意:

Apparently, there is a naming convention. Pay attention:

这不起作用:

// Controller
[HttpPost]
public ActionResult Send(BitcoinTransactionViewModel transaction)
{
}

// View
@using (Html.BeginForm("Send", "DepositDetails", FormMethod.Post, new { transaction = Model }))
{

@Html.HiddenFor(m => m.Token);
@Html.HiddenFor(m => m.Transaction.TransactionId);
.
.

这有效:

// Controller
[HttpPost]
public ActionResult Send(BitcoinTransactionViewModel **RedeemTransaction**)
{
}

// View
@using (Html.BeginForm("Send", "DepositDetails", FormMethod.Post, new { **RedeemTransaction** = Model }))
{

@Html.HiddenFor(m => m.Token);
@Html.HiddenFor(m => m.Transaction.TransactionId);
.
.

换句话说 - 命名约定错误!Model.Transaction 属性和我的 transaction 表单字段 + 控制器参数之间存在命名歧义.难以置信.

In other words - a naming convention error! There was a naming ambiguity between the Model.Transaction property and my transaction form field + controller parameter. Unvelievable.

如果您遇到同样的问题,请确保您的控制器参数名称是唯一的 - 尝试将其重命名为 MyTestParameter 或类似的名称...

If you're experiencing the same problems make sure that your controller parameter name is unique - try renaming it to MyTestParameter or something like this...

此外,如果您想将表单值发送到控制器,您需要将它们作为隐藏字段包含在内,然后就可以了.

In addition, if you want to send form values to the controller, you'll need to include them as hidden fields, and you're good to go.

这篇关于MVC 4 ViewModel 没有被发送回控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆