简体   繁体   English

MVC3:获取通过Javascript添加的控件的值

[英]MVC3: Get the value of controls added via Javascript

I added a textbox to my form via javascript as follows 我通过javascript将文本框添加到表单中,如下所示

<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 和JavaScript看起来像

javascriptFxn(name){
  document.getElementById('Other').innerHTML = '<br /> <input type="text" name="odd">';
 }

my Model 1 looks like 我的Model 1看起来像

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. 从提供的代码部分来看,您的Model(传递给视图)看起来不具有Odd属性,相反,模型具有Model1属性,而该属性又具有Odd属性。 Try having Odd property directly on your model and check if the value is posted when the form is submitted. 尝试直接在模型上具有Odd属性,并检查在提交表单时是否发布了该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM