MVC – Best practice of passing data from partial view to parent view

asp.net-mvcdesign-patterns

I am trying to create a partial view (re-used many times) that will prompt the user for data and return that value to the parent view.

My best solution so far is to pass to the partial view the name of a hidden object (a div, for instance) in the parent view, that the partial view will then set teh value of when the user has selected or entered a value.

Is this a bad design since the partial view is now dependent on the parent (i.e. that the object exists)?

If it is bad practice, what would be a better practice for creating re-usable data prompts?

Best Answer

There is no post-back occurring after the user selects the data that is to be passed back to the parent

Stop. Disconnect from everything server-side. You are dealing with client side functionality, and the solution to your problem will not have anything to do with C#, ASP.NET MVC, or Razor. Instead, JavaScript is what you need. It sounds like the basic user interaction is:

  1. User chooses something in a dropdown or enters something in a text field
  2. Another are of the page should change based on what the user selected or entered?

The solution will look something like this:

  1. JavaScript listens for the change event on one or more form fields
  2. When the user changes a value (selects a new item in a dropdown or types something different in a field) do something to some other, specific HTML tag elsewhere on the page

This might be a good candidate for a question on StackOverflow, but you'll need to give specifics about your problem.