Alternative of hidden fields Asp.NET MVC - asp.net

This question is more about asking suggestions over a scenario.
I have assigned a project built with MVC .NET. There are 100s of hidden fields on _Layout.cshtml page. These all hidden fields have url stored in them.
That means they all are loaded every time layout renders.
I want to optimize this situation by removing all hidden fields from layout.
To do this I am thinking of maintaining a local .JSON file which will have objectName same as id of hidden fields'.
So if a hidden field is -
<input type="hidden" id="myId" value="www.google.com"/>
Then .json goes to -
{
"myId":"www.google.com"
}
So It would be easy for me to access its value.
Is there anyway better I can do this?

instead why dont you create a singleton class and save them in that as a list and then whenever you need in view al you need is to just parse it to local javascript variable by #html.Raw(YOUSCingletonClass) and you will get all the values

Related

Selected files to upload vanish when form is invalid on submission

When a user selects a file to upload along with it and submits an invalid form, the file they selected disappears, while the remainder of the form remains intact. Is there a way to make it so that the selected file doesn't disappear upon an invalid submission?
I should include that I didn't make a ViewModel for this. They're two different models (the form/fields themselves, and the document info) being smushed together. Would making a ViewModel of the form with the document included in it prevent this from happening? (If so, I'd have to refactor most of my code unfortunately, which isn't the best option right now)
I was also considering throwing in unobtrusive validation via JS to bypass this, but I wanted to see some other potential solutions I could try.
Unlike other form controls, you cannot set the value for a <input type="file" /> in the controller. If you could it would be a major security issue (imagine navigating to a site that included hundreds of hidden file inputs with say "C:/Password.doc" - it would download files from your device without your knowledge).
If you need to return the view because the model is invalid, then the user needs to either re-select the file, or you need to save the file before returning the view (perhaps to a temporary location). You could then return the name and path of that file so that it could be displayed in the view to indicate that it has been uploaded (and perhaps an associated delete button so the user could delete it if they changed their mind.
Having a view model with properties for HttpPostedFileBase and the file name and its temporary path would make handling this easier.

How to upload file with form-post using c++ Qt-WebKit?

So, when i was dealing with forms before what i would do is find web element that is part of the form input, set value of that form input by element.setattribute("value", "infoi want to add")
then simply when i set value of form elements i click submit button of form.
Is this possible to do with web-form input type="file" and how can i do it?
Thanks
The simplest and the most straightforward way to do exactly what you've described is to get form's target address with QWebFrame::hitTestContent and then to post a file with QNetworkAccessManager::post. You also could inject some javascript code into a loaded page (see this for more details), but that would be an overkill for such a simple taks. And besides in the latter case all your injected javascript will be gone after submitting the form (i.e. after the page is reloaded).

ASP.NET Dynamic Data and uploading files

I am developing a small, internal-use only web application. Given its simple nature and intended audience, I decided that it might be a good opportunity to use a ASP.NET Dynamic Data project to get things up-and-running quickly. So far so good, except for one issue that has me reconsidering the whole plan:
I need to be able to upload files through the website. There is an entity in the model that represents an uploaded file. This entity has properties for the file's contents, the file name, and the file's content type. When uploading a file, all of these values are obtained from a single FileUpload control.
Since a FieldTemplate has a one-to-one association with an entity property, I decided that I needed to create a custom EntityTemplate for the File entity. At this point, I have created an "edit" template for the entity that has a FileUpload control. What I have not been able to figure out is, when the user clicks the 'Update' link, how do I get the data from the FileUpload control back into the entity and (ultimately) into the database.
Any advice or guidance is much appreciated.
you can add other values to the dictionary in the ExtractValues method of the FiedlTemplate, what you have to be aware of is that if those values also appear as rows or column in the page template then they will overwrite the values you added. I usually scaffold them as false and then only referance them in the custom field template.
Note you can access their initial values from the Row property in the OnDataBinding event, you can cast the property as the actual type or use an interface added via buddy classes.

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.Net nested form

I need to supply an html form (not a server form) for searching within an asp.net web-forms website. The form will post to another website where an indexed search is performed. Seeing as nested forms don't work well in asp.net, what is the simplest approach for this?
The nested form is a simple html form that performs a "get" against the search website, which I do not have programmatic control over.
Update: I resolved my issue by simply moving the server form to the appropriate place on the page, rather than having it surround the entire page. However, I'm still wondering how this would be handled if the html form needed to be placed physically between server controls (which require the server form).
However, I'm still wondering how this would be handled if the html form needed to be placed physically between server controls (which require the server form).
You can place controls on your page without requiring an HtmlForm.
In your case there's no issue declaring another form markup, but you could also just use some search control on your main form and make it issue a GET to that website.
Not only do nested forms "not work well," you basically can't have >1 form per page at all. The simplest approach is the approach you are forced to go with: write a page that only uses one <form runat="server"></form>. Since you need search functionality, is there no ASP.NET search box control that you could use?
Have a read here.
There are 4 workarounds:
Use an IFRAME
Force Submission to Navigate Using a GET Request
Dynamically Change the Form Action
Use a 3rd Party Form Handler
More details on http://www.revindex.com/Blogs/tabid/65/EntryID/21/Default.aspx
Nested forms don't work well in HTML full stop! You should never do it.
Perhaps you mean more than one form on page? Whilst it's true you can only have one form with runat="server", I can't see any reason why you couldn't have a standard form (not server form) that posted to another site at the same level (ie. not nested).
Try adding your HTML input elements to wherever you want the nested form to be. Then use JQuery to change the page form action to point to the external Website. The entire form is submitted, but with a different external Url. The minor downside is the values for all input elements on the page are posted, but most times that is not big deal.
(I only tried this using a POST, not a GET. This is basically Roman O's #3 workaround in more detail)
<div id="nested-form">
<input type="text" name="q">
<input name="searchbtn" value="Go" type="submit" class="search-button">
</div>
<script>
$(function() {
$("input.search-button").click(function() {
$('form').get(0).setAttribute('action', 'http://external.com');
});
});
</script>
maybe you try Server.Transfer() to your target page that do the search from a button for example!

Resources