My page uses two models. One for the main page rendering and another partial for search functionality.
The search view-model is retrieved using Ajax. Here is the model:
public class SearchViewModel
{
public int Id { get; set; }
public string Name { get; set; }
}
But my Razor view uses a different model (for main page rendering):
public class DifferentViewModel
{
public int Id { get; set; }
public string Value { get; set; }
}
I'm concerned if the correct Id value will be passed when posting data through the form where the hidden field holding the SearchViewModel.Id is located.
@using (Html.BeginForm("Action", "Controller"))
{
@(Html.HiddenFor<SearchViewModel, Int32>(m => m.Id))
}
Question: Is it possible to rename the attribute id in input to match the SearchViewModel? For example, something similar to:
<input type="hidden" id="SearchViewModel.Id" />
Alternatively, could I use an Attribute in the SearchViewModel to bind to a different id?
I looked at the link 1, below, but it didn't solve my question. Also, all the references I found redirect me to a custom model binder, which I wouldn't like to implement (since it would be easier to change the name of the property Id in the SearchViewModel).
Reference: