Asp.net mvc 简单ViewModel属性在表单提交时丢失值

Asp.net mvc 简单ViewModel属性在表单提交时丢失值,asp.net-mvc,asp.net-mvc-5,Asp.net Mvc,Asp.net Mvc 5,我有一个非常简单的例子,我正在尝试创建。单击一个按钮,我试图在视图模型中增加一个数字,就是这样。代码的全部内容如下: 视图模型 public class CustomerOverallViewModel { public int currentState { get; set; } } 控制器 public class WorkflowController : Controller { public ActionResult Index() { Cust

我有一个非常简单的例子,我正在尝试创建。单击一个按钮,我试图在视图模型中增加一个数字,就是这样。代码的全部内容如下:

视图模型

public class CustomerOverallViewModel
{
    public int currentState { get; set; }
}
控制器

public class WorkflowController : Controller
{
    public ActionResult Index()
    {
        CustomerOverallViewModel viewModel = new CustomerOverallViewModel();
        viewModel.currentState = 0;

        return View(viewModel);
    }

    [HttpPost]
    public ActionResult Index(CustomerOverallViewModel viewModel)
    {
        viewModel.currentState++;

        return View(viewModel);
    }
}
看法

当我点击按钮时,无论发生什么情况,currentState总是被重置。我错过了什么?如何使用页面上没有的变量?

给您:

控制器:

public class WorkflowController : Controller
{
    public ActionResult Index()
    {
        var viewModel = new CustomerOverallViewModel();

        return View(viewModel);
    }


    [HttpPost]
    public ActionResult Index(CustomerOverallViewModel viewModel)
    {
        viewModel.currentState++;

        //Per feedback
        ModelState.Clear();

        return View(viewModel);
    }
}
视图:

我个人会使用JavaScript并对您的操作进行一些Ajax调用

希望这有帮助


~Cheers

Html帮助程序使用ModelState的值,而不是模型属性的值,因此在发布时,currentState的当前值将添加到模型状态。增加其值不会更改ModelState的值。正确的做法是遵循PRG模式,但可以使用ModelState.Clear,然后再增加值。为了解释你的行为,我要试一试!永远不要将模型传递给GET方法!a如果模型包含复杂对象或集合的属性,绑定将失败,属性将为null。b您可以轻松地超过查询限制并抛出异常。它创建了一个非常难看的查询字符串。正确的方法是[HttpGet]public ActionResult Indexint currentState,其中POST方法将currentState的增量值传递给GET方法,并使用参数值初始化CustomerOverallViewModel的新实例。我完全同意您的观点,但在这种情况下,它是一个定义良好的非常小的模型。老实说,我相信最好的方法是利用Ajax,永远不要离开视图。另一个选项是在POST操作中缓存状态,并从缓存中获取值。通过这种方式,您可以将状态保留一段指定的时间。我怀疑OP只显示了模型和控制器的一部分,因为它的显示完全没有意义-表单中只有一个隐藏字段!是否使用ajax与这个问题无关——这就是为什么视图中没有更新更改的属性值。我认为将currentState传递给GET方法也不是一个好方法。在你看来,他的模型可能更复杂。是什么让你认为传递属性currentState的值不是一个好方法?您可以这样做,或者保存模型并将其ID传递给GET方法,然后再次从存储库中检索模型。这就是PRG模式的工作原理。
public class WorkflowController : Controller
{
    public ActionResult Index()
    {
        var viewModel = new CustomerOverallViewModel();

        return View(viewModel);
    }


    [HttpPost]
    public ActionResult Index(CustomerOverallViewModel viewModel)
    {
        viewModel.currentState++;

        //Per feedback
        ModelState.Clear();

        return View(viewModel);
    }
}
@model AgentWebsite.ViewModels.CustomerOverallViewModel

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

@using (Html.BeginForm())
{
    <div>
        called from the WorkflowController -> Index method

        @Html.Raw(Model.currentState)
        @Html.HiddenFor(m => m.currentState)

        <input type="submit" class="btn btn-success" value="Back" />
        <input type="submit" class="btn btn-success" value="Next" />
    </div>
}