2011-02-05 20 views
2

我有一個簡單的模型

namespace StackOverflow.Models 
{ 
    public class Test 
    { 
     public Test() 
     { 
      Name = "Test"; 
      Subs = new List<Sub>(); 
      Subs.Add(new Sub { Num = 1, SubName = "A" }); 
      Subs.Add(new Sub { Num = 2, SubName = "B" }); 
     } 

     public string Name { get; set; } 
     public List<Sub> Subs { get; set; } 
    } 

    public class Sub 
    { 
     public int Num { get; set; } 
     public string SubName { get; set; } 
    } 
} 

和相關視圖

@model StackOverflow.Models.Test 

<script src="@Url.Content("~/Scripts/jquery-1.4.4.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/JScript1.js")" type="text/javascript"></script> 

@{ 
    ViewBag.Title = "Test"; 
} 

<h2>Test2</h2> 

@using (Html.BeginForm()) 
{ 

    <p>Test Name</p> 
    @Html.EditorFor(m => m.Name) 

    <p>Sub 1</p> 
    @Html.EditorFor(m => m.Subs[0].Num) 
    @Html.EditorFor(m => m.Subs[0].SubName) 

    <p>Sub2</p> 
    @Html.EditorFor(m => m.Subs[1].Num) 
    @Html.EditorFor(m => m.Subs[1].SubName) 

    <div id="result"></div> 

    <br /> 

    <input type="submit" value="create" /> 
} 

與控制器的動作是

public ActionResult Test() 
    { 
     Test test = new Test(); 

     return View(test); 
    } 

    [HttpPost] 
    public PartialViewResult Test(Test test) 
    { 
     if (ModelState.IsValid) 
     { 
      return PartialView("_Results", new Result {Value = "Pass"}); 
     } 
     else 
     { 
      return PartialView("_Results", new Result { Value = "Fail" }); 
     } 
    } 

PartialView僅顯示文本字符串。

我使用下面的JavaScript做後

$('form').submit(function (e) { 
    $(':submit', this).attr('disabled', true); 
    e.preventDefault(); 


    $.post($(this).attr("action"), $(this).serialize(), function (data) { 
     $("#result").html(data); 
     alert("hurray"); 

    }); 
    return false; 
}); 

一切工作正常(和真正的代碼我有一個jQueryUI的模式對話框出現在提交點擊關閉時後恢復)

我想,我需要使用$就這樣我就可以有一個「錯誤:」功能刪除jQueryUI的模態對話框應該什麼地方錯了。這是我所遇到的問題和我的生活我不能工作了什麼,我做錯了

這是我第一次嘗試,我只是得到了警報(「錯誤」)出現在後。

$.ajax({ 
     url: $(this).attr("action"), 
     type: "POST", 
     data: $(this).serialize(), 
     datatype: "json", 
     contentType: "application/json; charset=utf-8", 
     success: function() { 
      alert("success!"); 
     }, 
     error: function() { 
      alert("error!!"); 
     } 
    }); 

我也曾嘗試通過以下爲 「數據:」

JSON.stringify($( 「形式」)serializeArray());

當我看到在控制器動作「測試」我看到,它不包含任何數據

我也試過只是傳遞

($("form").serializeArray() 

和剛剛得到的「錯誤」警報。

誰能幫我解決這個問題?

非常感謝。

回答

3

的問題是在這條線:

contentType: "application/json; charset=utf-8", 

這設置content-type標題爲請求。它沒有指定響應的任何內容。你與serialize創建數據字符串:這意味着,這將是在正常a=value&b=othervalue格式,它是強調 JSON。

取出contentType線。 jQuery將設置正確的請求頭(application/x-www-form-urlencoded

+0

非常感謝我現在得到傳遞給控制器​​動作正確的數據說明我也改變了數據類型在上面的代碼:。數據類型:「HTML」 – 2011-02-05 18:12:55