Hi I have the following Action method in my Controller class:
[HttpPost]
public IActionResult MyAction(string foo, Guid? bar)
{
if(!bar.hasValue)
bar = Guid.NewGuid();
var viewModel = new MyViewModel { Foo = foo, Bar = bar };
return ViewComponent("MyDialog", new {model = viewModel});
}
And my view component is:
@model MyViewModel
<div>
@Html.HiddenFor(m => m.Bar)
<span>@Model.Bar</span>
</div>
This was fine when bar
has value during calling MyAction()
, if bar
is null
then the value of the hidden tag generated does not set to Guid.NewGuid()
. However the <span>
tag can always display the correct value from the ViewModel passed.
Why?