I'm writing the partial view for the child collection ('Vehicles') of my model that renders each element in a table row. My model is therefore in the form of
#model IEnumerable<OrderVehicleViewModel>
For modelbinding to work I need the name of each element to be in the form
Vehicles[0].LicenceExpiry
If I use EditorFor then names are generated in the form
item.LicenceExpiry
If I set the HtmlAttributes with a name value it is seemingly ignored, to get around this I have to do
#Html.TextBox("Vehicles[" + i + "].LicenceExpiry", item.LicenceExpiry)
The problem is then, I lose all the formatting as TextBox doesn't adhere to the DisplayFormat attribute on my model and it means I have to manually build SelectLists for simple boolen properties whereas the default template used by EditorFor does it for me. What is the best way to achieve this?
Related
I have created a base viewmodel that all of my view models inherit from. That part is easy.
All views are bound to a viewmodel (all are inherited from the base view model)
Within the OnActionExecuted method I insert a true/false value onto a property within the baseviewmodel depending on some conditions.
From the view side of things. I have a single layout page that some how I want to be able to read the property's value and render a different partial view based on the value.
Is this possible? I dont want to have to add the code to each of the views but I don't think I should be binding the layout to my baseviewmodel either.
If I can stay away from inserting the value into the valuebag that would be great as I need to be able to access these values anywhere in the application via strongly typed names.
what you want probably isnt possible because as you call a view from the controller then first the code inside that view is executed and then the layouts code is executed
to achieve what you are doing you can do 2 things
1. make the logic inside the controller itself and then render the correct view
2.call the layout from the controller giving it the name of the partial view in some property of the model or in the viewbag
Not sure I quite follow the use case, but rather than trying to render out a partial view, have you thought about nesting your layout pages.
I think you should be able to override the layout in the onactionexecuted, so you can set the layout dependant on the bool and that layout will render only the correct option.
Look here for an example:
Nested layout pages with Razor
HTH
Si
I have a model that contains a collection property as List(). In my view, each of the ChildModel objects is rendered in a table row via it's own Editor Template.
On postback, my model gets bound correcty in that contains a list of ChildModels with the correct about of items originally created in the view, but all the properties of the ChildModel are null, with the exception of the single Editable field.
Based upon this, it appears as if it is creating an entirely new collection of objects, and forgetting all about any values set previously.
I've tried adding the other child object properties to hidden fields, but I still get default values (nulls or zeros) for all properties accept for the one I have created an edit control for. (textbox).
Any thoughts on how to get at a minimum a key field for my item so I can simply update my existing collection?
I found that my hidden field value is in the Request.Form on postback with the Quantity values as well as the values I put in the Hidden fields. I'm not sure why the FormValueProvider isn't parsing those values.
I have a Model object with a List collection of sub objects. I need to load the form fields for the sub object to the page, and provide a link to dynamically add form fields for a second object. So in other words, multiple sub objects can be created via one form and one post back.
My first thought was just to put the form fields in a partial view and load the view via Ajax.ActionLink. This worked but the problem comes in when trying to uniquely identify each object in the collection and bind the collection of objects on postback. For this it seems the correct usage would be to use an #Html.EditorFor() helper, but I don't know how to call that via Ajax to dynamically add the object's editor template to the page when the link is clicked.
It sounds like you were on the right track with regards to a partial view, you'll just need to sort out the name property of your inputs so you can correctly bind to a collection of objects.
Have a look at this article by Phil Haack explaining how binding to lists works, or google for something more up to date with razor syntax.
MVC #Html.TextBoxFor(model => model.SomeNotNullableType) When using this helper, if I have a type that is not nullable in the database the view ends up with a default value in the text box. Specifically in my case, the field takes an integer, and it's putting a default value of 0 in the field.
I know I could remove the erroneous 0 with Javascript or setting the default value in html, but I was hoping there was a more 'correct' way of doing this. Can I specify in my model that I don't want a default view to be put into the text box (you cannot set it to null, as it is not nullable). Sticking with the true model approach I'd like to fix it in the model/controller, rather than in the view.
perhaps adding a viewmodel for that model class with a nullable int property (and all other properties for that class), and then using data annotations to make it a required field would serve the purpose you're looking for.
you would then just need to roll the viewmodel back into your data persistence class in your controller.
You could create a custom editor (partial view) for the int type in which you create the textbox and you can set the value to when it's the default value.
Some of the forms represent parent objects and I want most field disabled in each of these forms, if they're for a 'parent' object. Is there an easy way to do this?
The easiest way is to give the fields a particular semantic class, and then disable all the ones with that class using jQuery. Assuming the forms are all contained in some larger container with an id of formgroup and you use dependent-field as the class name, you can use something like:
// Set the 'disabled' attribute on every dependent field in this group of forms.
$('#formgroup').find('.dependent-field').attr('disabled', true);