I added a textbox to my form via javascript as follows
<input type="text" name="odd" id="odd" />
and I added a parameter in my model
public string odd {get; set;}
Now, I wish to retrieve the text in this textbox in the controller by calling the variable odd, but it is always empty. Is there something I need to do to bind these two together besides giving them the same name?
EDIT
My form looks like
using (Html.BeginForm("ControllerName", "Home", FormMethod.Get, new { enctype ="multipart/form-data" })){
<div> @Html.DropDownListFor(m => m.model1.mode,
new SelectList(Model.model1.mylist, "Value", "Text"), "Select", new { @onchange="javascriptFxn(this.options[this.selectedIndex].text);"})
<div id="Other"></div>
}
and the javascript looks like
javascriptFxn(name){
document.getElementById('Other').innerHTML = '<br /> <input type="text" name="odd">';
}
my Model 1 looks like
public class model1{
public string odd {get; set;}
}
and the submit for controller
public ActionResult Submit(MyModel model)
{
string s = model.model1.odd;
}
From the portions of the code provided, it looks like your Model (being passed to view) does not have Odd property, instead the the model has a Model1 property that in turn has Odd property. Try having Odd property directly on your model and check if the value is posted when the form is submitted.