2012-04-02 49 views
1

我在我的aspx標記中有一個表單。如何在表單動態構建時使用Ajax表單?

我給它添加了一些動態的字段。

我將它們提交給控制器的操作。

我應該如何在服務器端讀取它們?

更新

  1. 我的表單包含n文本框。

  2. 我曾經想過用一個模型將在客戶端包含 IEnumerable<FormItem>

我會用Ajax表格吧。

有意義嗎?

+0

您是否嘗試過FindControl(controlName)? – 2012-04-02 15:14:59

回答

0

我曾經想過用一個模型將包含一個IEnumerable

是啊,偉大的想法。所以,一旦你(根據您的需要)定義的FormItem類,你都已經準備好了:

public class FormItem 
{ 
    public string Value { get; set; } 
} 

然後:

<form id="myform" action="/" method="post"> 
    <!-- Those inputs could be added dynamically --> 
    <input type="text" name="[0].Value" /> 
    <input type="text" name="[1].Value" /> 
    <input type="text" name="[2].Value" /> 
    <input type="text" name="[3].Value" /> 

    <button type="submit">OK</button> 
</form> 

最後AJAXify形式:

$(function() { 
    $('#myform').submit(function() { 
     var form = $(this); 
     $.ajax({ 
      url: form.attr('action'), 
      type: form.attr('method'), 
      data: form.serialize(), 
      success: function(result) { 

      } 
     }); 
    }); 
}); 

請確保您結帳Phil Haack's blog post有關綁定到列表的預期連線格式。

Steve Sanderson的blog post post關於編輯可變長度列表也是必讀。

+0

'<%for(int i = 0; i <3; i ++){%>'如果我不知道有多少表單在那裏? – 2012-04-03 07:30:11

+0

服務器端的默認聯編程序如何處理'form.serialize()'?作爲一個List? – 2012-04-03 07:43:29

0

假設你的表單包含該用戶的文本框輸入一個電子郵件地址,併爲您的形式標記看起來是這樣的:

@using (Html.BeginForm("Index")) 
{ 
    <!-- pretend this field was dynamically created with javascript --> 
    <input id="email" type="email" name="email" /> 

    <button type="submit">Submit</button> 
} 

email文本內的值可以通過訪問在Request對象的Form屬性:

public class HomeController : Controller 
{ 
    [HttpPost] 
    public ActionResult Index() 
    { 
     // Get the value of the input named `email` 
     var email = Request.Form["email"]; 

     /* Do cool stuff because you can */ 
    } 
} 

或者,你可以修改你的操作方法,以接受一個字符串參數具有相同的名稱作爲輸入:

public class HomeController : Controller 
{ 
    [HttpPost] 
    public ActionResult Index(string email) 
    { 
     // The default model binder will set the value of the `email` parameter to the 
     // value of the matching input from your form 

     /* Do cool stuff because you can */ 
    } 
} 

還有其他的方法,比如接受FormCollection類型(見this example)的參數,或創建一個視圖模型類的強類型

+0

謝謝。你能否詳細說一下'新模型類'。這是最有趣的,但它的動態。我該如何填寫客戶端列表? – 2012-04-02 19:54:28

0

你可以使用jQuery,如果你的領域被動態創建然後我想你會以數組形式傳遞這些字段。 您應該爲每個新字段添加一個類,以便您可以獲取它們。

<input class="dynamicInput" type="text"></input> 
<input class="dynamicInput" type="text"></input> 
<input class="dynamicInput" type="text"></input> 
<button id="submit">submit</button> 
<script type="text/javascript"> 
    $(function(){ 
     var myArray = []; 
     $('#submit').on('click', function(i,val){ 
      //It will go through any input with a class "dynamicInput" 
      $.each($('.dynamicInput'), function(){ 
       myArray.push($(this).val()); 
      }); 
      //We'll send our array to MyAction 
      $.post('/MyController/MyAction', myArray, function(data) { 
       //If your action returns a string you could handle it through "data" 
       console.log(data); 
      }); 

     }); 
    }); 
</script> 

那麼你的行動會得到這個數組througn HTTP POST請求的JavaScript對象,你需要將其反序列化到C#數組,所以你可以在你的服務器端處理:

[HttpPost] 
public ActionResult MyAction(FormCollection values) 
{ 
    var jsonArray = values["myArray"]; 
    var deserializer = new JavaScriptSerializer(); 
    var cSharpArray = deserializer.Deserialize<List<string>>(jsonArray); 


    //Here you will handle your array as you wish 


    //Finally you could pass a string to send a message to your form 
    return Content("Message to user"); 
} 
+0

想要做「回傳」來保存redudant重定向。無論如何我都會重定向到其他頁面。你有想法嗎? – 2012-04-02 19:51:50

+0

你可以有兩個同名的動作,一個用於獲取請求,另一個用於發佈請求。如果您使用jQuery post方法,它會對您的發佈操作發佈發佈請求,控制器會完成他的工作,然後當完成時,您可以在回調函數中處理它,而不必重定向到其他網址。 – 2012-04-02 21:13:53