POST之后丢失的数据视图模型 [英] Losing ViewModel Data after POST

查看:96
本文介绍了POST之后丢失的数据视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白这个问题常常但我有一个使用布局.cshtml。在布局我有:

I don't see this problem too often but I've got a .cshtml that uses a layout. In the layout I've got:

@using (Html.BeginForm(null, null, FormMethod.Post, new { @class = "someCssClass", @id = "UserForm" }))
{
      ...rest of the code
}

我用这个布局主要.cshtml在顶部定义为我们总是这样的模式:

My main .cshtml using this layout has the model defined at the top as we always do:

@model CarViewModel 
@{
    Layout = "~/Views/Shared/_CarLayout.cshtml";
}

当它得到回到我的操作方法,我得到了模型的所有值空值:

When It gets back to my action method, I get nulls for all values of the model:

public ActionResult Cars(CarViewModel model)
{
    carBL.RemoveCars(model.CarIds, model.DealerId);
    ...
}

不知道我需要做和为什么发生这种情况。通常我只是拿回来通过成功autobind。在我看来,当模特是通过在markup-剃须刀被调回罚款与返回的视图模型中使用,但如果我不使用这些字段,它不...所以我认为这是如何工作的,如果我不使用他们加价,我需要给他们发回的隐藏价值则因为我没有使用从视图模型x字段中(这将自动持久这些领域,如果我在的形式使用它们来强制持久性)?

Not sure what I need to do here and why this is happening. Usually I just get it back successfully via autobind. It seems to me when the model is used via RAzor in the markup- that gets posted back fine with the returned ViewModel but if I'm not using those fields, it doesn't...so I assume that's how that works and if I don't use them in mark-up I need to send them back as hidden values then to force the persistence since I am not using x fields from the ViewModel (Which would have automatically persisted those fields if I had used them in the form)?

推荐答案

如果该值不绑定到表单字段,他们会回来空。

If the values are not bound to a form field, they will come back null.

在表单中使用下面的东西像ID字段。

in the form use the below for things like ID fields.

@Html.HiddenFor(x => x...)

这篇关于POST之后丢失的数据视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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