ASP.NET MVC Drop Down List - c#

I am making some changes to already existing ASP.NET MVC 1 app where there is a form with a dropdown list that has all 50 states hard coded into the HTML. After filling out the form the user can later go back and edit their information. I want to make it so that on the edit screen the value that is already in the DB gets the "selected" attribute for the state. The only way I can think to do this is to build the html on the server and send it down to the view, is there a better practice?

No, View is the only place where you should generate the markup.
My suggestion is to write a JavaScript snippet like this:
$(function(){
$('#dropDownId').val(#Model.SelectedValue);
});
It's effective, easy, maintainable, and of course, fast.

Related

.NET MVC Dynamically add/remove items without using javascript

I am on a C# .NET MVC project, and have a form that can dynamically add/remove n number of complex objects in a list. This complex object, for example represents a Person. This person has FirstName and Address properties.
When the user loads the page, all the People in the system are displayed in a list. When the user presses the 'add' button, two new text boxes show up for the Person's FirstName and Address properties. When the user presses the submit button, it will make a POST request to the server.
I know that you can write regular html in the View, and can use Javascript to add the new DOM elements for the FirstName and Address properties.
And with regards to when the user submits, I can use javascript to scrape all the data in the screen, and send a POST request to the server. Theoretically, another method is instead of using javascript, just make the button submit the form to the POST action of the controller; if I give my DOM elements the proper name attribute, the Action should recognize the data.
However, is there a MVC way of doing this? Maybe, with the help of Razor Helpers?
I am not sure what you are asking here; is it how to remove and add lists to your table or is it how to make a post?
I'll try to answer both:
1: Post without javascript:
Your assumption that you can post by naming the properties is correct! However, the page will have to reload if you want the new person in your table. Reading your description, I don't think this is really what you are looking for.
2: Add a row without javascript:
This cannot be done. You need javascript for this or a javascript framework which uses data-binding (there are a lot)
I would strongly advise you to make a decision on if a page reload is a deal-breaker or not for your solution. If it is, try to solve your issue with jQuery or better yet: investigate in javascript frameworks which use data-binding (knockoutjs, angular, react, etc...). There is a learning curve to these frameworks but they make your developer life so much easier and increase the maintainability of your code.

How working with fields out from DetailsView

I'm newbie in ASP.Net C#.
I made a site on Classic ASP, but I need to recreate on .NET. I am using DetailsView on detail page.
I need to move some fields to another place on mi page. Example: usually put the name on page top, email place is on top too but right side inside a DIV formatted with bootstrap.
I don't know how separate fields that are grouped on DetailsViews's table.
All information on MS site talks about DetailsView, this stuff it's cool but I want more flexibility for placing fields anywhere. DetailsView's table makes me feel tied.
Can you add the section of your code your trying to change? May be easier to understand your question.

repeater with custom filters in asp.net

I have repeater control in which i am binding data from sql server database, Now i have number of records showing on my page but i want that i can filter my records according to dropdown selected. I need 4-5 dropdowns control thats depend on my need and i don't want my page to refresh while filtering.
Below is the example, this website using checkboxes too but i need only dropdowns...
http://www.phonearena.com/phones/full#/phones/full/
Give me any idea how to start, i think ajax is the thing i need. Suggest some examples if you have.
Ajax is the right solution.
Personally i suggest you jquerytemplate and ajax call few row code to achive your goal you may find more info here Load JSON data using the dropdown menu and refresh the div area with the new results in a web site
It is a good point to start and you can make more stuff with this logic :) really simple and fluid code :)
I would suggest you to use a client-side framework (so in JavaScript) like AngularJS, BackboneJS or KnockoutJS. They all provide ways to work with collections in JavaScript (like filtering in this case). You won't need Ajax excepted for the first load or if you want to do some paging. And because it's all client-side it will be far more fluid than re-downloading at every criteria change.

How to update labels in the same asp.net page by C#

I wrote a page which user can input a name and get some infos from the database. In the .cs file I got the texts from the database and assigned them to the labels and in the debugging mode, the labels did change their texts. BUT I don't know how to update them in the same page. I used some ways to update the page, but it updated the whole page and display nothing in the labels.
How can I achieve this function?
I google it and found the AJAX is a good way, but it's an emergency i have no time to learn AJAX?
does someone have good idea to help me solve it?
Thanks a lot!
What you're talking about is a postback. When the page posts back to the server, the page is refreshed.
If you want to set the labels and avoid losing the data with a postback, you can do so through an ajax call in your JavaScript code, you can set hidden fields or you can set the values in the Session object (not the best idea). There are numerous ways around this; you just have to pick one.
Do some reading on ajax (it's not as hard as you think). You can call the server through ajax, which will get the data from the Db and return it to your JavaScript as JSON. You can then use that to fill your labels.
You may want to look into an UpdatePanel as well. They aren't the fastest solution available, but they are very easy to implement.

jQuery Core/Data or Custom Attributes(Data-Driven)

A similar question was asked here in storing information in a given html element.
I'm still green to jQuery, but I'm looking for the best way to store information on the page. I have a Repeater that holds one image per item. These images are clickable and can fire a given jQuery event. The issue I'm having is, the objects that the Repeater is bound to holds some specific information(such as "Subtext", "LargerImage", etc) which I would like to be accessible from the page.
Core/Data in jQuery accomplishes this just fine, however we would still need to build the jQuery statement from C#, as all the data is stored on the server. To clarify a bit, this is storing information on the page from a database, which is a bit different than arbitrary information being made available through jQuery.
I'm not restricting this question to "how to bind a custom attribute to an element", because I did come across an idea of generating a JS Struct from the C# codebehind to store information, but I'm avoiding any code generating code scenarios(or trying to).
Custom Attributes from HTML5(ie, "data-subtext") are also a possibility as I can easily add those from the itemdatabound event:
sampleImageElement.Attributes.Add("data-subtext", "And this what the image is about");
I'm a bit confused on browser support for this specific attribute though, or if it is even best practice so early in the game. If custom attributes are the way to go, that's an easy change to make happen. If jQuery can accomplish the same, I'd love to be pointed that way at least for my own understanding.
Any thoughts are greatly appreciated.
I'm answering this question only for the record keeping purposes of stackoverflow, as this is the solution I've moved forward with for this scenario. An AJAX call is completely warranted for any larger datasets and is a direction I would definitely go otherwise.
I went ahead with the "data-" field in the HTML5 spec, provided by the jQuery meta-data plugin.
I'm wrote a short extension method on the Web.UI.AttributeCollection class called "AddMetaData", which accepts an IList as well as a string "Key" to ease the attachment to a given page element.
I'm not marking this as the answer just yet, as there might be some community feedback on my own direction.
To clarify what happens in ASP.NET, once the page is served to the client, the objects that the Repeater is bound to on the server are destroyed and are then recreated upon each page postback.
It sounds like you want to achieve some kind of tooltip effect where the contents are retrieved from the server through AJAX? There are numerous different tooltip options available
jQuery Tooltip plugin
Random.Next()'s jQuery AJAX tooltip
dhtml goodies AJAX tooltip
clueTip
that can be used to do this. You could then set up a webservice or page method to retrieve the relevant data from your datasource.
Of course, you could have the content rendered in the HTML sent to the client when the request is processed and simply hide this markup. Then write your own plugin to display the markup in the form you require.

Categories

Resources