Passing Complex object from View to Controller: one object is always null - asp.net-mvc-2

I'm passing a complex object as a Model to the View as
but when I get the Model back from the View, one particular object comes always null while other complex types are normally passed through
my View is the default Edit Strongly Typed View
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>
Even renaming the Action to
[HttpPost]
public ActionResult General2(GeneralInfo model)
gives the same error

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.

Related

angularjs2: setting fields to "dirty", especially datepicker

I am new to Angular2/Typescript, since I come from the Java world, I decided to learn Typescript and Angular2 directly.
I want to leave most of the logic on the server, thus I don't need complex validation management on the client. So all I want is the user to fill out forms, and post/put all the fields to the REST Service.The goal is to leave the client side as light as possible.
I have a form:
<form role="form" (ngSubmit)="onSubmit()" #ArbeitstagForm="ngForm">
and a field in it, some datepickers too: similar like this:
<input type="text" class="form-control pull-right" id="datepicker" [(ngModel)]="model.datum">
When I submit the form, I call this function:
model = new Arbeitstag();
onSubmit(form:any) {
alert(JSON.stringify(this.model));return false;
}
So that alerts me the the entered data as JSON, which I will after send to a REST Service. It works actually great, BUT only when I actually type something into the field, when I have a default value, or I set the field with a datepicker, the model object values will remain empty.
I've found out about the dirty setting of the fields, which are false by default and are getting true when I type something in and that's also what I see when I check firebug, but that's definitely not what I want to achieve.
Is there a way to set all the fields dirty in a form in Angular2? I've found many examples for Angular.js 1, but not for Angular2/Typescript.
Control has a markAsDirty() (and markAsTouched()) method
<input #datePicker="ngForm" type="text" class="form-control pull-right" id="datepicker" [(ngModel)]="model.datum">
<button (click)="datePicker.control.markAsDirty()">update dirty status</button>
Plunker example
What I usually do is get a reference to the form in my component, using ViewChild. With that reference I can mark to form dirty or touched when I need to. Like so:
export class MyComponent implements OnInit, OnDestroy {
#ViewChild('form') form: NgForm;
...
public methodWithFormChange(): void {
this.form.control.markAsDirty();
}
}
;-)

Spring #ModelAttribute doesn't care about commandName

JSP:
<form:form commandName="editWeather" method="post" action="../edit">
<!-- Input fields -->
<input type="submit" value="Submit">
</form:form>
And this is how I get the model in Spring:
#ModelAttribute("DONTGIVEADAMN") Weather weather
And I can still use the weather to do my operations and it works great, for example:
weatherService.editWeather(weather);
My question is...Why does this work?
Model attribute name doesn't matter when binding data received from a form (because names of form fields correspond to the names of fields of the model object), it matters only when rendering a form.
I particular, when model attribute name in your POST handler method doesn't match the commandName in the form, you will be able to receive the data, but won't be able to redisplay a form with validation errors.
its matching the class type (or interface), not the name of the variable/parameter; and the specified request mapping/method signature must be correct.

ASP.NET MVC 2 and lists as Hidden values?

Hi,
I have a View class that contains a list, this list explains the available files that the user have uploaded (rendered with an html helper).
To maintain this data on submit I have added the following to the view :
<%: Html.HiddenFor(model => model.ModelView.Files)%>
I was hoping that the mode.ModelView.Files list would be returned to the action on submit but it is not?
Is it not possible to have a list as hiddenfield?
More information : The user submit a couple of files that is saved on the service, when saved thay are refered to as GUID and is this list that is sent back to the user to render the saved images. The user makes some changes in the form and hit submit again the image list will be empty when getting to the control action, why?
BestRegards
Is it not possible to have a list as hiddenfield?
Of course that it is not possible. A hidden field takes only a single string value:
<input type="hidden" id="foo" name="foo" value="foo bar" />
So if you need a list you need multiple hidden fields, for each item of the list. And if those items are complex objects you need a hidden field for each property of each item of the list.
Or a much simpler solution is for this hidden field to represent some unique identifier:
<input type="hidden" id="filesId" name="filesId" value="123" />
and in your controller action you would use this unique identifier to refetch your collection from wherever you initially got it.
Yet another possibility is to persist your model into the Session (just mentioning the Session for the completeness of my answer sake, but it's not something that I would actually recommend using).
Before I start I'd just like to mention that this is an example of one of the proposed solutions that was marked as the answer. Darrin got it right, here's an example of an implementation of the suggested solution...
I had a similar problem where I needed to store a generic list of type int in a hiddenfield. I tried the standard apporach which would be:
<%: Html.HiddenFor(foo => foo.ListOfIntegers) %>
That would however cause and exception. So I tried Darrin's suggestion and replaced the code above with this:
<%
foreach(int fooInt in Model.ListOfIntegers)
{ %>
<%: Html.Hidden("ListOfIntegers", fooInt) %>
<% } %>
This worked like a charm for me. Thanks Darrin.

Html.Hidden() inserting wrong value

When I use a form html helper method in one of my views like <%=Html.Hidden("id", "some id text") %> it creates a hidden input field for me but it puts the wrong value in there.
Instead of getting
<input name="id" type="hidden" value="some id text"/>
I get
<input name="id" type="hidden" value="11000"/>
So the value is being found from somewhere else. In this case it's the primary id of the parent record. So it is an id, it's just the wrong id.
Does anyone have any ideas? I'm pretty sure this didn't happen in MVC1
Model binding always takes precedence. The model binder doesn't know of if a field is hidden. See http://forums.asp.net/t/1559541.aspx and
http://forums.asp.net/t/1703334.aspx
I can thing about following options
Value you are passing to the view is wrong, eg. data passed to view(you can check debugging in controller to see what are you passing into it
You are using ViewData and TempData data with different values which are overridden
else please put here your code so we all can see what is wrong

ASP.NET MVC 2: What Model property datatype will autobind an html select (DDL) with multiple selections?

The customer has a Model property that requires a comma separated list of selected options. We present their select list (DDL) as a multi-choice drop down.
What would the property datatype look like that would autobind multi-selections in the client side HTML select (DDL)?
The select posts data like this:
myOptions=Volvo&myOptions=Mercedes&myOptions=Audi
And we want to automagically bind it back to some property:
IList<string> CarChoices {get;set;}
So the POST action method parameter would be (Carform myForm)
which would have myForm.CarChoices which includes a List of the three selected cars?
I might be misunderstanding what you're trying to accomplish but I think this post from Phil Haack describes how to do what you're attempting to do in a clean way: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
Sometimes it is just easier to get your hands dirty and work with the HTML. I suggest doing something like this:
<select multiple>
<% foreach(var item in Model){ %>
<option value="<%= item.ID %>"><%= item.Description %></option>
<% } %>
</select>
obviously your model is your collection. You can also use the ViewData["Whatever"] object to pass data as well, your choice.