0

I have a form on my view:

@using (Html.BeginForm("ClearData", "MemberPass", FormMethod.Post))
{
    <div>
        @foreach (var property in ViewData.ModelMetadata.Properties)
        {            
            @Html.Hidden(property.PropertyName, property.Model)
        }
    </div>
    <button>Clear</button>
}

and the following action methods:

public ActionResult Index()
{
    MemberPassInfoViewModel memberPassInfoViewModel = new ServiceUtilities().GetEventDetails(DateTime.Now.Date);
    return View("Index", memberPassInfoViewModel);
}

public ActionResult GetMemberPassInfo(MemberPassInfoViewModel currentMemberPassInfoValues)
{
    MemberPassInfoViewModel updatedMemberPassInfoViewModel = new ServiceUtilities().GetMemberPassInfoViewModel(currentMemberPassInfoValues);
    return View("Index", updatedMemberPassInfoViewModel);
}

public ActionResult ClearData(MemberPassInfoViewModel currentMemberPassInfoValues)
{
    MemberPassInfoViewModel updatedMemberPassInfoViewModel = new ServiceUtilities().ClearData(currentMemberPassInfoValues);
    return View("Index", currentMemberPassInfoValues);
}

In debug I can see that the model's two properties are present. However when I view the generated HTML, one of the properties has a blank value:

<input id="SearchCode" name="SearchCode" type="hidden" value="23" />
<input id="FullName" name="FullName" type="hidden" value="" />

I noticed that this could be fixed by replacing:

@Html.Hidden(property.PropertyName, property.Model)

with:

<input type="hidden" value="@property.Model" name="@property.PropertyName" />

Which generates:

<input type="hidden" value="23" name="SearchCode" />
<input type="hidden" value="Alex Robert" name="FullName" />

Why does the @Html.Hidden() method not work, but explicitly writing the <input> tag does work?

David Klempfner
  • 8,700
  • 20
  • 73
  • 153
  • 1
    You need to show your controller methods (most likely your method has a parameter named `FullName` and its using the value from `ModelState`) –  Mar 01 '18 at 00:28
  • I just updated the question with the action methods. – David Klempfner Mar 01 '18 at 00:48
  • That would not be happening if your navigating to the `Index()` method, so I assume it when your navigating to `GetMemberPassInfo()` or `ClearData()`? And in those requests, you are passing a value of `23` for `SearchCode` and omitting a value for `FullName`? –  Mar 01 '18 at 00:52
  • Yes that's correct. When I debug the @Html.Hidden(property.PropertyName, property.Model) I can see that property.Model = "Alex Robert", but then the generated HTML has Value="" – David Klempfner Mar 01 '18 at 00:54
  • Start by reading [this answer](https://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111) to understand the behavior –  Mar 01 '18 at 00:55
  • I had a read of that. I understand that if ModelState.IsValid = false, then it will display the previous value. But shouldn't property.Model be the same value regardless of if it's in a helper method or used in ? – David Klempfner Mar 12 '18 at 09:42
  • 2
    Sorry, not sure what your saying. When you use `@Html.Hidden()`, the method uses the value from `ModelState` which in you case is an empty `string`, not the value of the property (which is "Alex Robert") as explained in the link. When you use `` you are using the value of the property - there is no `ModelState` involved –  Mar 12 '18 at 09:48

0 Answers0