Asp.Net C# MVC Dynamic Forms (Changing Dom structure and getting data on the server)

前端 未结 3 560
失恋的感觉
失恋的感觉 2021-01-24 10:07

I dynamically change DOM on client-side to add some new input fields using JavaScript.

Can I obtain the data on the server-side without using Ajax? Just pushing send but

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-24 10:51

    If you're posting the form, sure. You can use either the FormCollection to get the element you want, or explicitly identify the parameter in your method signature. Assume you added an input element with the name "myTextBox", you could do the following:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(int id, string myTextBox, FormCollection collection) {
    
      // better
      if (myTextBox != null) {
        // do something with the string
      }
    
      // good
      if (collection["myTextBox"] != null) {
        string textboxvalue = collection["myTextBox"].ToString();
      }
    
    }
    

提交回复
热议问题