Get values of hidden fields in controller (ASP.NET MVC) - asp.net

Can I reach the value of hidden fields in controller action ? And how ? Do I put it in the model somehow ?
Thank you
EDIT: some code example how to store something in hidden field and retrieve it on postback would be appreciated.

Hidden fields are just like any other form fields when you get to the POST action being sent to the server. There's nothing that makes them special outside of their display (or lack thereof, rather) in the browser. Thus, they should be accessible in much the same way you currently access other form fields.
How do you currently access other form fields? If they're just mapped to a model which is being passed into the controller action, then hidden fields should be mappable just the same.
Conversely, you should be able to manually access any HTTP POST data from HttpContext and the Request object.

Related

Many forms for editing entity on one page

I have a list of objects on my page. I need to edit an object in a popup.
There are many objects, and generating many forms for each object is not correct.
What can I do, can an iframe do in a popup?
You don't need to use iframe.
Build your form in your controller and render it in the html. The fact that it's in popup doesn't change anything. It doesn't matter if it's in popup or not, the final result will be POST call to your action.
You should have an action that renders a form, callable from an ajax in the view where you have all those entities.
Just change the entity id that will be received in the action as argument by changing the ajax url using js depending on the clicked entity you want to edit.
Then return with the ajax the form already rendered so with only one form you can edit as many entities (of the same class) as you want, without even need to render one form before they click which one they want to edit.

Passing values that aren't related to inputs using MVC?

I have page where users can select from one or more images. When they are done I would like them to navigate to the next page and what is displayed would be based on the selection from the previous page.
"Selecting" just means that they click the image and it has a CSS class added to it. When they click the link to navigate to the next page I'd like to collect the images that have been selected and pass that information along using either TempData or Session.
In most of the examples I have seen either inputs or the query string is used to pass information from the View to the Controller. How can I pass which elements have a particular class to my controller when a link is clicked?
If you're using a link click, I'd probably just append the selected images to the query string. I'm assuming you don't mind exposing this query string to end users.
I'm sure that's probably not the answer you were looking for. But as you stated I think you're only to legitimate options are passing the values through the query string or using hidden inputs and posting the page to your action by intercepting the link click event.
You said you do not want to post back to servers. You cannot access TempData or Session without posting back to server, so they are out of scope.
You only have client side option, so you want to collect a user's selected items in array.
Once the user clicks to Next Page, you create a query string like this ?ids=1-2-3-4 and retrieve those value at next page.
Other thoughts: Long URL likes this is a bit ugly, and URL has maximum length limit depending on browser. If I'm you, I'll post back to server to collect the selected values. Then use TempData (or some persistent storage).

How do you send a controller action to a partial view in lieu of a model?

So I'm trying to add a partial view to my main view in MVC3, but the partial view needs new data. Instead of expanding the view model that has the necessary data in the main view and then passing it along to the partial view, is it possible to specify a controller action that directly feeds the partial view with the necessary model?
For example something like:
#Html.Partial("_PartialView", Controller, Action, Parameters)
Thanks in advance.
In a limited sense, yes.
The only thing you can do is send the current model over to another action through Html.Action
Besides that you either need to add it to TempData, or pass what's required in the querystring through your GET parameters OR use an ajax request where you write these values to an html form and serialize that to your new page, but thats a hack : )

View state vs Hidden field in asp.net

How can we take decision for viewstate and hidden field in ASP.NET.
In my case i am using page cross post back and by using public properties of first page i am accessing them in second aspx page.
After getting public variable in second aspx page i need to access those value in second page but as soon as i do postback in second page, i am not able to find those value.
Hence to solve this issue i have two solution either use viewstate in second page or using hidden field in second page.
I am not able to decide which one should i use?
The approach is quite the same. Only difference should be the size of stored info (viewstate is using [sometimes encrypted] base64 while hidden fields use plain text unless you encode them yourself), and viewstate allows you to make sure the data was not tampered with thanks to the default validation it has in place.
If the data is small and you want to manipulate the value based on some client-side behaviour, hidden field will be useful.
Difference between view state and a hidden field in asp.net
http://royalarun.blogspot.in/2012/03/difference-between-view-state-and.html
Both are used to store the value during the postback in asp.net , but
In View state - not able to change the value by Client side code i.e java script.
Hidden field - possible to change value by Client side code.
In View state - You can store more than one value like Datatable and Dataset
Hidden field - You can store more than one value in hidden field,by serialized it.
View state data is encrypted and Hidden field is not encrypted

ASP MVC form fields wrongly getting filled in

I have an ASP MVC form which updates a business object and then its action goes to another page.
Both of these pages have a textarea with an id "Description", when it goes to the other page, for reasons unknown it fills in the values which were entered before the form submission.
Which method are you using to update your object? Have you tried adding the object type name in the ID?
Like, Product.Description instead of just Description.
Not knowing what your doing exactly, I'd say there is a 'Description' property on the ViewData's Model. ASP.NET MVC will try to match up your form values with values from the ViewData, including the Model. You can rename the control or reference the object directly as Chad said. eg Product.Description
With more details, someone may be able to help more.
Without seeing your controller actions this is a bit of guess work but how does it go to another page?
If in the post controller you return a different view (or return another action that returns a view etc) it may be that the ModelState is using the attempted values from the previous form submission, this is the expected behaviour (it's how the validation system and model binding works for a start). If you are sending them to a different form use a RedirectResult to your next action and use TempData if needed for any state you need to keep while transitioning.
If at all possible I'd also recommend using the strongly typed helpers such as Html.TextAreaFor(x=>x.Description)
<p><label>Description</label><br /><br /><%=Html.TextArea("Description", new { style = "width:470px;" })%></p>
For clarification, I'm going from an "Edit project" page, to a "Add issue" page

Resources