0

I have a dropdownlist like this:

var Country = new List<ListItem> 
{ 
        new ListItem { Text = "American" }, 
        new ListItem { Text = "British" } ,
        new ListItem { Text = "Spanish" }, 
        new ListItem { Text = "Persian" } ,
        new ListItem { Text = "China" },
        new ListItem { Text = "else" }
};
@Html.DropDownList("Country", new SelectList(Country))

When a user choose “else” , one textbox appears and user can type it’s country on textbox, I did this by jquery :

@Html.TextBox ("txtCountry",null,new {@id="txtCountry"})

I want to define a variable to get Country from user and send to database. Filed’s name in Model is “Country”

How do this?

2
  • Why do you mean when you say "I did this by jquery"?? did you create the control in jquery with the name txtcountry?? Commented Oct 12, 2013 at 8:28
  • no, I just appear and hide textbox by jquery Commented Oct 12, 2013 at 9:01

1 Answer 1

1

You can get the list of form controls values using FormCollection Class. Try the below option

Note : Your controls should have have names (just having an id is not returning values in the formCollection)

[HttpPost]
public ActionResult YourActionMethod(FormCollection Collection)
{
        string Country = string.Empty;

        if (Collection["txtCountry"] != null)
            Country = Collection["txtCountry"].ToString();
//Else you can assign the values to your model object.
        return View();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.