本文介绍了HTTP POST MVC中的渲染​​后查看相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图重新发布一个看法,但即时得到空到我想要的领域。

Trying to repost a view but im getting null to the field I want.

继承人的控制器...

Heres the controller...

    public ActionResult Upload()
    {
        VMTest vm = new VMTest();
        return View(vm);
    }

    [HttpPost]
    public ActionResult Upload(VMTest vm, String submitButton)
    {
        if (submitButton == "Upload")
        {
            //do some processing and render the same view
            vm.FileName = "2222";           // dynamic creation of filename
            vm.File.SaveAs(@vm.FileName);   // save file to server
            return View(vm);
        }
        else if (submitButton == "Save")
        {
            //read the file from the server
            FileHelperEngine engine = new FileHelperEngine(typeof(PaymentUploadFile));
            PaymentUploadFile[] payments = (PaymentUploadFile[])engine.ReadFile(@vm.FileName);  // the problem lays here @vm.FileName has no value during upload

            //save the record of the file to db
            return View("Success");
        }
        else
        {
            return View("Error");
        }
    }

我已经有一个@ Html.HiddenFor(型号=> Model.FileName)我的观点里。

I already had a @Html.HiddenFor(model => Model.FileName) inside my view.

但我仍得到了一个空值Model.FileName。

But still I got a null value for Model.FileName.

任何帮助PLS

感谢

推荐答案

如果你打算修改POST操作您的视图模型的一些价值观,你需要先删除从ModelState中的旧值:

If you intend to modify some values of your view model in the POST action you need to remove the old value from modelstate first:

ModelState.Remove("FileName");
vm.FileName = "2222"; 

这样做的原因是,HTML辅助,如文本框,隐...结合后,在您的视图模型中的值时,在ModelState中首先使用的值。

The reason for this is that Html helpers such as TextBox, Hidden, ... will first use the value in the modelstate when binding and after that the value in your view model.

此外,而不是:

@Html.HiddenFor(model => Model.FileName)

你应该使用:

@Html.HiddenFor(model => model.FileName)

注意在EX pression小写 M

这篇关于HTTP POST MVC中的渲染​​后查看相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 04:39