Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Complex object from View to Controller: one object is always null

I'm passing a complex object as a Model to the View as

alt text

but when I get the Model back from the View, one particular object comes always null while other complex types are normally passed through

alt text

my View is the default Edit Strongly Typed View

alt text

What am I missing?

The ModelState Error says

The parameter conversion from type 'System.String' to type 'Julekalender.Database.CalendarInfo' failed because no type converter can convert between these types.

Why don't I get the same for the other types? How is it automatically converted?


I have added 3 fields (as the T4 template does not append this types) but I still get null when POSTing

The green boxed below is the field

<div class="editor-field">
    <%: Html.TextBoxFor(model => model.Calendar.Guid)%>
</div>

alt text


Even renaming the Action to

[HttpPost]
public ActionResult General2(GeneralInfo model)

gives the same error

alt text

like image 745
balexandre Avatar asked Nov 05 '22 06:11

balexandre


1 Answers

Make sure that when you use this wizard there are input fields generated in the view for each property of the Calendar object so that when you post the form they will be sent to the controller action. I am not sure this is the case (haven't verified if the wizard does it for complex objects, I've never used this wizard).

In the resulting HTML you should have:

<input type="text" name="Calendar.Prop1" value="prop1 value" />
<input type="text" name="Calendar.Prop2" value="prop2 value" />
... and so on for each property you expect to get back in the post action
... of course those could be hidden fields if you don't want them to be editable

UPDATE:

The problem comes from the fact that you have a string variable called calendar in your action method and an object which has a property called Calendar which is confusing. Try renaming it:

[HttpPost]
public ActionResult General2(string calendarModel, GeneralInfo model)

Also don't forget to rename it in your view.

like image 83
Darin Dimitrov Avatar answered Nov 12 '22 18:11

Darin Dimitrov