0

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:

BeginForm in ChildAction uses wrong id

Community
  • 1
  • 1
Marlos
  • 1,899
  • 2
  • 22
  • 44
  • How would the incorrect Id be posted? Unless you are using a single form with both values... but then that is the real issue. – Erik Philips May 20 '14 at 18:13
  • You are right, @ErikPhilips, but I'd still have problems in jQuery searching for a single element, this way: `$("#Id")`, right? In my example, I'd have two elements with ` – Marlos May 20 '14 at 18:17
  • 1
    This seems to have an underlying design/architectural flaw that you are trying to work around. Can you show the code how you are passing the two models to the page? – Erik Philips May 20 '14 at 18:23
  • 1
    What is the signature for action in the controller? – Yogiraj May 20 '14 at 18:42

0 Answers0