ASP.NET MVC 1.0 nested models with partial views

I am trying to correctly bind a nested model to a nested view, but I'm out of luck.

Here is a detailed overview

This is class

public class Foo { public AnotherClass AnotherClass { get; set; } public string Name { get; set; } public ...... } 

Inside AnotherClass, we have more subelements like

 public class AnotherClass { public AThirdClass { get; set; } } 

The third class has properties that we want to bind to.

 public class AThirdClass { public string ImportantString { get; set; } public string SecondString { get; set; } } 

The main view expects a class of type Foo. Inside this view, we call the html helper to display the partial view that the model of the AnotherClass type that we are passing expects. The call will be

 <% Html.RenderPartial("MyPartialView", Model.AnotherClass); %> 

Inside the partial view of MyPartialView, there are text fields for editing fields in AThirdClass, and they are configured as follows

 <%= Html.TextBox("AThirdClass.ImportantString", Model.AThirdClass.ImportantString) %> 

When we send back to the server, I lose all the data that was entered in the text box. Is this not supported in MVC 1.0? Can I use this technique if I don't have partial views when using nested objects?

It looks like in MVC 2.0 you can use the EditorFor HTML helper to do what I need, but I'm stuck on MVC 1.0.

What am I doing wrong?

+4
source share
3 answers

I have a job for this.

If you pass the full view model to a partial view, and then reference the helper objects directly from the top model, then you will be fine.

I would love to hear someone else do it anyway.

+4
source

I'm on MVC 3, still having the same problem, switched to less perfect inheritance. If someone knows a better way, write about it.

+2
source

I solved this using the code below. The problem is that the identifiers generated for the partial view templates do not comply with the MVC model binding rules. Therefore, try using the partial view option, as shown below.

 <% Html.RenderPartial("MyPartialView", Model.AnotherClass, new ViewDataDictionary(){ TemplateInfo = new TemplateInfo() { HtmlFieldPrefix = "AnotherClass" } }); %> 
0
source

Source: https://habr.com/ru/post/1309581/


All Articles