HiddenFor description is:
Returns an HTML hidden input element for each property in the object that is represented by the specified expression.
I read that it is useful for fields in your Model/ViewModel that you need to persist on the page and have passed back when another call is made but shouldn't be seen by the user.
HiddenInput description is:
Represents an attribute that is used to indicate whether a property or field value should be rendered as a hidden input element.
Can you please tell me when is HiddenInput useful? and when to use it instead of Html.HiddenFor?
Thanks!
HiddenFor is determined by the view - the view creates a Hidden input value from any element of the viewmodel or data.
HiddenInput is an attribute on a field in the (view)model - which means that it can be used across multiple views and indicates that this field should be hidden wherever it is rendered by an EditorFor helper
Related
I need to send parameters with values in a BeginForm html.
Example:
# using (Html.BeginForm ("Create", "IncomeDeclaration", new {declarationAmount = document.getElementById ("element"). value}))
This value I can not get from the Model, as it is not found in the model.
I've tried several ways and nothing worked. If you could change the content of a ViewBag that'd be great.
I appreciate your support.
You can't mix and match Javascript (document.getElementById ("element")) with the C# form declaration. If you want a value submitted with the form, you should add a relevant form element inside the form declaration. If you don't want a regular form element (eg. a textbox), you can use a "hidden" input field. If you want, you can dynamically populate the hidden field using javascript.
I'm working on my first MVC application, still figuring it all out.
I have a viewmodel which, at this point, is identical to my domain object. My controller builds the viewmodel and passes it to a view. The view only displays a some of the properties because I don't want primary and/or foreign keys displayed for the user yet, in the case of a primary key, I need to have the data in order to update / delete the database.
It appears that unless I use a viewmodel property in the view, it is set to default values (0 for numeric value types and null for reference types) when I pass the viewmodel back. Is this correct behavior?
I confirmed the viewmodel passed to the Edit view does contain all the properties (as I would expect).
The question - Once a view is rendered, what happens to the viewmodel? If my viewmodel contains properties that are not used in the view, do their values just disappear? For example, when I click the Edit actionlink to fire the Edit action on the controller, the viewmodel that gets passed to the action does not contain any properties unless they are visible on the screen. Why?
BTW, this is ASP.NET MVC 4 RC.
It appears that unless I use a viewmodel property in the view, it is
set to default values (0 for numeric value types and null for
reference types) when I pass the viewmodel back. Is this correct
behavior?
Yes, when you invoke a controller action you need to pass all the properties that you want to be bound in the request. So for example if you are using a html <form> to call the action you need to use input fields. You could use hidden fields but they must be present, otherwise nothing is sent to the controller action
The question - Once a view is rendered, what happens to the viewmodel?
It falls out of scope and is eligible for garbage collected.
If my viewmodel contains properties that are not used in the view, do their values just disappear?
Absolutely. But even if you use those properties inside the view they disappear. For example if you only display the values inside the view but do not use input fields to send them back to the server when the form is submitted they will be gone as well.
For example, when I click the Edit actionlink to fire the Edit action
on the controller, the viewmodel that gets passed to the action does
not contain any properties unless they are visible on the screen. Why?
Because the view model no longer exists. It's gone and garbage collected. That's how the HTTP protocol works. It's stateless. Nothing is persisted between the requests. You will have to include whatever properties you want to be populated in the request either as POST form values or as query string parameters if you are using action links or whatever.
If the user is not supposed to modify those values inside the view you could then simply pass an id to the controller action which will allow for this controller action to retrieve the model from wherever it is stored (a database or something) using this id.
If your properties are in any helper methods which generates the input HTML element inside a form, It will be available in your HTTPost action method when you submit the form. If you are simply displaying it in a div/span , it is not going to get you the property values. That is how MVC model binding works.
Expect the values in your HttpPOST action if you use these HTML helpers
#Html.TextBoxFor
#Html.DropDownFor
#Html.EditorFor
#Html.HiddenFor
Dont expect the values if you use these
#Html.DisplayFor
In my controller I have the following:
ViewBag.VendorName = vendorname;
In my View I have the following:
#Html.TextBox("VendorName")
It appears that the TextBox VendorName is being populated with the content of ViewBag.VendorName.
This is what I want but didn't know that from a ViewBag you can directly populate a TextBox.
Is this expected and why does this happen?
According to Steve Sanderson in Pro MVC:
HTML helper methods populate their value from the following places (in this order):
ViewData.ModelState["VendorName"].Value.RawValue
For string based helpers, the value parameter passed to the helper method, or if you didn't supply one, then ViewData.Eval["VendorName"] (and ViewBag.VendorName as you're seeing here)
For strongly typed helpers, the corresponding property value on your Model object
I have a fairly complex ViewModel containing decimal properties, which are exposed to the user in the form of text boxes. I want a textbox with no value to be interpreted as zero. (The properties in the underlying domain object are non-nullable, and the default value is 0.)
When the DefaultModelBinder binds the view data to the ViewModel, decimal properties with blank strings for inputs are initialized to zero (as is standard in .NET), but the DefaultModelBinder is adding errors to the ModelState for the blank text boxes. As a result the ModelState is invalid and the user sees a whole bunch of "A value is required." errors for the textboxes they left blank.
How can I stop these errors from being added to the ModelState?
The best thing that you could do in this situation is to create a ViewModel. Instead of binding directly to your Domain Model, instead bind to the ViewModel that was created solely for the purpose of Data Transfer to your view. On the ViewModel, you can create these fields as nullable decimals. You can then map the ViewModel back to your Domain Model however you like.
This is really the correct behavior. If you enter nothing in the TextBox then that is equivalent to null, not 0.
I understand that fields such as Html.TextBox() accept two values, the first one being the name and the second one being the value. And so does Html.TextArea(). But in a case where the form is submitted as AJAX and the div where the form is placed is replaced with a view from the server, the form fields insist on taking the previous values. An image is worth a thousand words:
image http://img132.imageshack.us/img132/4171/aspnetmvcbug.png
I've checked everything on the controller and the model and the image is from debugging the view itself. The model is empty but the fields generated from it take the value of the previous submission.
The postback data is held in the ModelState. The built in HtmlHelper methods will look for values stored in the model state based on the name of the form element when rendering their content.
Check the View.ModelState property. Forms can grab values from there in certain circumstances.
Do you have an entry ViewData["Body"]? MVC will also attempt to bind a control to a ViewData item based on the name.