Title: dynamically generate same dropdownlist during run-time from user input
Tags: c#;jquery;.net;asp.net-mvc
Question: I got a question here about the said title.
Lets say I have already populated a dropdownlist like so:
<%: Html. DropDownListFor ( m =&gt; m. CategoryMenu , Model. CategoryMenu, "Select Category"  )%&gt;
and I have a button that says "Add new category" ( I am trying to add another category with the same source )
And I need to be able to collect all the data entered by the user.
So for example, if the user has added another category, I should be able to get the two different value during postback.
Would I be able to complete it using jquery?
Are there any provisions from .net c# that would be able to do this?

Here is the accepted answer: You could do this as an ajax request with a JSON response like so:
<script type="#text/javascript"&gt;
$(document).ready(function{
$("#MyAddCategoryForm").submit(AddNewCategory);
});
function AddNewCategory()
{
var categoryTitle= $("#MyCategoryTextField").val();
var data = {categoryTitle:categoryTitle};
$.post("/Category/Add/", data, function(data, response){
if(response == "success")
{
var option = $("<option&gt;</option&gt;").html(categoryTitle).val(data.Value);
$("#MyCategoryDropDown").append(option);
}
});
return false;
}
</script&gt;
Controller code:
[HttpPost]
public JsonResult(string categoryTitle)
//Insert category into DB
//And retrieve unique ID as "value"
int value = //add to db
return Json(new {Value = value});
Forgive any errors please I am doing this off the top of my head without actually implementing it but it should be pretty close to the mark.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2011:00:03/14/11', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><jquery><.net><asp.net-mvc>', 'Title': 'dynamically generate same dropdownlist during run-time from user input', 'ViewCount': 473, 'id': 5294686, 'user_AboutMe': None, 'user_AccountId': 721097.0, 'user_DisplayName': 'Nicholas', 'user_Reputation': 3.0, 'user_Views': 0.0, 'user_id': 658217.0}}