Help us understand the problem. What is going on with this article?

ASP.NET Core MVCでhiddenの値を変更する

More than 1 year has passed since last update.

概要

POSTされたhiddenの値を変更しても、その内容が変更内容が反映されない。
ContollerでModelStateをClearすると反映されるようになる。
詳細は下記を参照。

環境
.Net Core 2.2

サンプルコード

Controller
public IActionResult Index()
{
    var vm = new ViewModel { Value = 1 };
    return View(vm);
}

[HttpPost]
public IActionResult Index(ViewModel vm)
{
    vm.Value = 2;
    return View(vm);
}
ViewModel
public class ViewModel
{
    public int Value { get; set; }
}
View
@model ViewModel

<!DOCTYPE html>
<html>
<head></head>
<body>
    <form method="post">
        <input asp-for="Value" type="hidden" />
        <span>@Model.Value</span>
        <button type="submit">送信</button>
    </form>
</body>
</html>

対応前実行結果

POST前と後でspanの値は変わっているが、hiddenの値は変わっていない。

POST前
<!DOCTYPE html>
<html>
<head></head>
<body>
    <form method="post">
        <input type="hidden" id="Value" name="Value" value="1" />
        <span>1</span>
        <button type="submit">送信</button>
    </form>
</body>
</html>
POST後
<!DOCTYPE html>
<html>
<head></head>
<body>
    <form method="post">
        <input type="hidden" id="Value" name="Value" value="1" />
        <span>2</span>
        <button type="submit">送信</button>
    </form>
</body>
</html>

対応内容

Controller
[HttpPost]
public IActionResult Test(ViewModel vm)
{
    ModelState.Clear();
    // もしくは
    // ModelState.Remove("Value");
    vm.Value = 2;
    return View(vm);
}

補足

リストデータの場合、POST前に存在するインデックスの値は変更が判衣されない。

Controller
public IActionResult Index()
{
    var vm = new List<ViewModel> 
    {
        new ViewModel { Value = 1 },
        new ViewModel { Value = 2 }
    };
    return View(vm);
}

[HttpPost]
public IActionResult Index(List<ViewModel> vm)
{
    vm = new List<ViewModel> 
    {
        new ViewModel { Value = 3 },
        new ViewModel { Value = 4 },
        new ViewModel { Value = 5 }
    };
    return View(vm);
}
View
@model List<ViewModel>

<!DOCTYPE html>
<html>
<head></head>
<body>
    <form method="post">
        @for (var i = 0; i < Model.Count; i++)
        {
            <input asp-for="@Model[i].Value" type="hidden" />
            <span>@Model[i].Value</span>
        }
        <button type="submit">送信</button>
    </form>
</body>
</html>
POST前
<!DOCTYPE html>
<html>
<head></head>
<body>
    <form method="post">
            <input type="hidden" id="z0__Value" name="[0].Value" value="1" />
            <span>1</span>
            <input type="hidden" id="z1__Value" name="[1].Value" value="2" />
            <span>2</span>
        <button type="submit">送信</button>
    </form>
</body>
</html>
POST後
<!DOCTYPE html>
<html>
<head></head>
<body>
    <form method="post">
            <input type="hidden" id="z0__Value" name="[0].Value" value="1" />
            <span>3</span>
            <input type="hidden" id="z1__Value" name="[1].Value" value="2" />
            <span>4</span>
            <input type="hidden" id="z2__Value" name="[2].Value" value="5" />
            <span>5</span>
        <button type="submit">送信</button>
    </form>
</body>
</html>
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away