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

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

问题描述

我似乎无法弄清楚如何将整个视图模型送回控制器到验证并保存功能。

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.Time transaction.Transaction.Location transaction.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 = GT; m.Transaction.TransactionId);
它的工作原理和我得到的控制器上的令牌属性,但是当我将其添加回来,交易的所有属性控制器上的对象是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,我一直工作在别的东西一遍bumpend到同样的问题。
只是这一次我想出如何使它工作!

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 财产和我的交易表单字段+控制器参数之间的命名歧义。 Unvelievable。

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视图模型没有被送回控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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